[Python-ideas] (try-except) conditional expression similar to (if-else) conditional (PEP 308)
g.brandl at gmx.net
Fri Aug 7 08:30:14 CEST 2009
> Jan Kaliszewski wrote:
>>> Syntax Ideas:
>>> Option 1:
>>> x = float(string) except float('nan') if ValueError
>>> op(float(string) except float('nan') if ValueError)
>> I wouldn't mix up 'if' word with 'except' -- there are different concepts.
>>> Option 2:
>>> x = float(string) except ValueError: float('nan')
>>> op(float(string) except ValueError: float('nan'))
>> IMHO seems to be the best of the propositions. I'd add to it optional
>> as (like in present 'try:... except Exc as exc:...')
> Can there be multiple 'except's?
> reciprocal = 1.0 / float(string) except ValueError: float('nan')
> except ZeroDivisionError: float('inf')
Don't over-complicate. This proposal has a slim chance of success as it
is; piling on top isn't going to help.
IMO, any syntax that puts something other than the exception class after
"except" has zero chance. The "except-then" is the most attractive to
me, but still doesn't read as naturally as "if-else".
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.
More information about the Python-ideas