[Tutor] Try except really better than if?
ALAN GAULD
alan.gauld at btinternet.com
Mon Jan 10 10:39:42 CET 2011
By the way I have this piece of code using elementTree standard module
>and according to Alan this is bad code I guess:
>
>
Which just proves there are no absolute rules in programming :-)
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
>Since you just use pass to ignore the error then you are kind
of forced to do it this way. Its not bad each statement has to
be treated individually, it is not a block where all the statements
must pass or fail as a unit.
> Do you have an alternative or my way is ok?
This would also be a valid case for an if/else check but
in readability terms I think try/except still wins because
the handler is only a pass. If it were more complex I'd
probably opt for if/else.
I know there is no special case which is enough special to break the rule
;o)
>I'm not sure I agree. In programming you can usually find a special case
that validly breaks any "rule"! Even "Don't use goto!" :-)
HTH,
Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110110/c19fe583/attachment.html>
More information about the Tutor
mailing list