[Python-ideas] [Python-Dev] (try-except) conditional expression similar =?iso-8859-1?q?to=09?=(if-else) conditional (PEP 308)

Steven D'Aprano steve at pearwood.info
Thu Aug 6 17:14:15 CEST 2009

Moved to python-ideas.

On Thu, 6 Aug 2009 09:32:16 pm Antoine Pitrou wrote:

> I have to say that the original example:
>     x = float(string) except ValueError else float('nan')
> looks artificial. I don't see how it's adequate behaviour to return a
> NaN when presented with a string which doesn't represent a float
> number.

By memory, the IEEE standard allows for 254 different NANs. Back in the 
80s or 90s, SANE (Standard Apple Numerics Environment) supported 
different NANs for different errors, and I'm pretty sure that one of 
them was for failed string-to-float conversions. In any case, the idea 
seems perfectly sensible to me.

As for the suggestion, I'm not convinced by the examples given either. 
I'm not a huge fan of the `x if y else z` construct -- I thought I 
would be, but in practice I nearly never use it -- and I think this is 
just more of the same. Seems good in principle, but in practice, I'm 
not sure it is actually that readable, and there are usually perfectly 
adequate alternatives.

-0 from me.

Steven D'Aprano

More information about the Python-ideas mailing list