HowCanI: inlined exceptions
mjhand at concentric.net
Sat May 27 08:07:19 CEST 2000
Pete Shinners wrote:
> what i'd really,really like to do is define something
> that works like this
> def catch(expression, errcode=None):
> return expression:
> return errcode
> if __name__ == '__main__':
> name = catch(fields, 'Anonymous')
> i would really like to do this for myself, because i
> would use it extensively!
Personally, I think the nicest, most readable, and most Pythonic
way to write it is:
try: name = fields
except: name = 'Anonymous'
But if you want anything cleaner than that, and which does not
involve passing the code to be evaluated into a function with
a try:except:, you'll have to wait for Guido to implement
something like this:
name = fields except 'Anonymous'
so that we can use the "or" idiom in a slightly different way. :-)
I gotta admit, I would make use of that if it was there...
More information about the Python-list