inline exception handling in python

MRAB python at mrabarnett.plus.com
Thu Aug 12 14:42:12 EDT 2010


wheres pythonmonks wrote:
> 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?
> 
Discussed a year ago:

[Python-Dev] (try-except) conditional expression similar to (if-else) 
conditional (PEP 308)

http://code.activestate.com/lists/python-dev/90256/




More information about the Python-list mailing list