inline exception handling in python

wheres pythonmonks wherespythonmonks at gmail.com
Thu Aug 12 19:45:12 CEST 2010


Hi!

I have on a few occasions now wanted to have inline-exception
handling, like the inline if/else operator.

For example,

The following might raise ZeroDivisionError:

f = n / d

So, I can look before I leap (which is okay):

f = float("nan") if d == 0 else n/d;

But, what I'd like to be able to write is:

f = n / d except float("nan");

Which I find much more appealing than:

try:
   f = n / d
except:
   f = float("nan")

(Obviously, I am thinking about more complicated functions than "n/d"
-- but this works as an example.)

Thoughts?

W



More information about the Python-list mailing list