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

P.J. Eby pje at telecommunity.com
Thu Aug 6 03:20:54 CEST 2009

At 05:59 PM 8/5/2009 -0700, Raymond Hettinger wrote:
>[Jeffrey E. McAninch, PhD]
>>I very often want something like a try-except conditional expression similar
>>to the if-else conditional.
>>An example of the proposed syntax might be:
>>    x = float(string) except float('nan')
>>or possibly
>>    x = float(string) except ValueError float('nan')
>+1 I've long wanted something like this.
>One possible spelling is:
>   x = float(string) except ValueError else float('nan')

I think 'as' would be better than 'else', since 'else' has a 
different meaning in try/except statements, e.g.:

    x = float(string) except ValueError, TypeError as float('nan')

Of course, this is a different meaning of 'as', too, but it's not 
"as" contradictory, IMO...  ;-)

More information about the Python-Dev mailing list