HowCanI: inlined exceptions
Manus Hand
mjhand at concentric.net
Sat May 27 02:07:19 EDT 2000
Pete Shinners wrote:
>
> what i'd really,really like to do is define something
> that works like this
>
> def catch(expression, errcode=None):
> try:
> return expression:
> except:
> return errcode
>
> if __name__ == '__main__':
> name = catch(fields[1], '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[1]
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[1] 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...
But-I-wouldn't-necessarily-recommend-holding-your-breath-ly-y'rs,
Manus
More information about the Python-list
mailing list