HowCanI: inlined exceptions
Robert Citek
rwcitek at uci.edu
Fri May 26 18:37:41 EDT 2000
Hello Pete,
Is this something of what you have in mind?
def catch(expression, errcode=None):
try:
return eval(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'
print name
print age
print pet
- Robert
-----
At 02:09 PM 5/26/00 -0700, Pete Shinners wrote:
>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