How best to write this try/except block?

Brian Quinlan brian at sweetapp.com
Wed Apr 3 11:52:49 EST 2002


> try:
>     value = d[key]
>     foo = stuff (value)
> except KeyError:
>     foo = otherStuff (key)
>

try:
    value = d[key]
except KeyError:
    foo = otherStuff (key)
else:
    foo = stuff (value)

OR:

if value.has_key(key):
    foo = stuff (value)
else:
    foo = otherStuff (key)






More information about the Python-list mailing list