HowCanI: inlined exceptions

Pete Shinners pete at visionart.com
Fri May 26 17:09:53 EDT 2000


after working with python a bit, one thing i find myself
needing to do really often is test an expression before
i can use it one simple assignments/etc

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')
    age = catch(now-birth, 100)
    pet = catch(findpet(name, age)) or 'No Pet'

i would really like to do this for myself, because i
would use it extensively! i understand the problem 
here is that my "expression" is being evaluated 
before the function is getting called, so the
exception is raised outside of my function.

i'm just hoping there's some way to do this be
defining a lot of 'magic' classes and default
handling. i could make it work by passing the
expression in quotes (so it is a string), but
i'd prefer something a little cleaner

this is a case where C-style macros are handy!
but i know some python guru can come through
for me in this case.    (???)

-- 
_____Pete Shinners______
   pete at visionart.com



More information about the Python-list mailing list