catching exceptions in expressions
Vaclav Dvorak
dvorakv at idas.cz
Wed Aug 6 19:20:53 EDT 2003
Hello, Sheilas and Bruces!
I found myself writing something like this lately:
try: a = int(s)
except ValueError: a = 99
try: b = int(t)
except ValueError: b = 99
try: c = int(u)
except ValueError: c = 99
Not enough to make me want to create a function, but enough to be
annoying. :-) I was thinking about something like this:
a = int(s) except ValueError then 99
or
a = int(s) or 99 if ValueError
or even a different approach:
try:
a, b, c = int(s), int(t), int(u)
except ValueError:
retry with 99
or yet another:
a, b, c = 99, 99, 99
try:
a = int(s)
b = int(t)
c = int(u)
except ValueError:
continue [execution on next line - my comment]
I like the first one the most. Imagine:
lambda x:
(A/x except NameError then 1/x) except ZeroDivisionError then 99
Catching exceptions in lambdas - how do you like that? :-)
Comments? Should I write a PEP? Am I missing something obvious? Has this
been debated a million times over already?
Cheers,
Vaclav Dvorak <dvorakv at idas.cz>
More information about the Python-list
mailing list