[Tutor] Try except really better than if?

Karim karim.liateni at free.fr
Mon Jan 10 10:12:13 CET 2011


Thank you Steven, Modulok and Alan for your precious and detailed 
explanations!

I understood that I must not overuse try-except statement and usually 
when the exception could happen exceptionally.
By the way I have this piece of code using elementTree standard module 
and according to Alan this is bad code I guess:

*try: devdb.setDescription(dev.attrib['description'])
except KeyError: pass
try: devdb.setSymbolName(dev.attrib['symbolName'])
except KeyError: pass
try: 
devdb.setSymbolPinOrderMappingList(dev.attrib['symbolPinOrderMappingList'])
except KeyError: pass
try: devdb.setLayoutName(dev.attrib['layoutName'])
except KeyError: pass
try: devdb.setLayoutType(dev.attrib['layoutType'])
except KeyError: pass
*
In fact elementTree *attrib* dictionary attributes could not have 
optionally xml attribute (depending on my xml input file).
Indeed to test existence I try to execute *dev.attrib['/<KEY>/'*]. But I 
can't use only one except
because the other settings could not be processed if one given optional 
xml attribute is not present (except will be raised).
I must did it for each possible attribute.

Do you have an alternative or my way is ok?
I know there is no special case which is enough special to break the 
rule ;o)

Regards
Karim

On 01/10/2011 03:10 AM, Alan Gauld wrote:
> ctions are slower than static functions, but that 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110110/15013c93/attachment.html>


More information about the Tutor mailing list