The web is full of discussions about what's best and more pythonic:
- try, except
or
- hasattr
In your example, the provided answer has 3 hasattr and I guess that the "else" clause is missing. The keys of the dictionary of the class are being accessed 3 times before doing anything.
With the:
try:
info = result.FirstField.SomeTable.SomeData.somethingorother.DooDah
except AttributeError:
info = somethinElse
you also have a nice way of dealing with it, by letting the code go straight into fetching values from the dictionary. The advantage is that if any field down the chain is missing, you catch it, without worrying about extra "hasattr" in the response.
I guess it depends more on preference and the willigness to be exception-aware or work more in a standard programming way.
Best regards
Daniel
On Mon, Sep 13, 2010 at 04:02, Rob D robzyb@gmail.com wrote:
Excellent :) Thank you. That's much nicer than the try/except I was using.
Rob.
Brandon Pedersen wrote:
if hasattr(result, 'FirstField') and hasattr(result.FirstField, 'SomeTable') and hasattr(result.FirstField.SomeTable, 'SomeData') and so on: info = that thing
On Sun, Sep 12, 2010 at 3:21 AM, Rob D robzyb@gmail.com robzyb@gmail.com wrote:
Hey, a big thanks to Jeff for helping me figure out how to use the Doctor :) It was much appreciated.
I currently have a bit of a problem, though. I have looked through the documentation and can't seem to find out what object client.service returns. This is my code:
result = client.service.DoSomethingIncrediblyInteresting(WithADiscoStick) info = result.FirstField.SomeTable.SomeData.somethingorother.DooDah
The thing is that sometimes FirstField doesnt exist which causes my Python program to quit rather angrily. Is there a check I can do on result, FirstField, and so on, to check of the children I'm about to grab info from actually exist?
Thanks, Rob.
suds mailing listsuds@lists.fedoraproject.orghttps://admin.fedoraproject.org/mailman/listinfo/suds
_______________________________________________
suds mailing listsuds@lists.fedoraproject.orghttps://admin.fedoraproject.org/mailman/listinfo/suds
suds mailing list suds@lists.fedoraproject.org https://admin.fedoraproject.org/mailman/listinfo/suds