inline exception handling in python

wheres pythonmonks wherespythonmonks at gmail.com
Thu Aug 12 20:54:07 CEST 2010


On Thu, Aug 12, 2010 at 2:42 PM, MRAB <python at mrabarnett.plus.com> wrote:
> 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/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


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

Nice -- excellent discussion and what I was looking for.  I am
guessing that no implementation materialized.



More information about the Python-list mailing list