[Python-ideas] (try-except) conditional expression similar to (if-else) conditional (PEP 308)
MRAB
python at mrabarnett.plus.com
Fri Aug 7 02:41:08 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)
>
> -0.5
> 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'))
>
> +0.5
> 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')
(We can already chain '==', etc.)
>> Option 3:
>> x = float(string) except ValueError else float('nan')
>> op(float(string) except ValueError else float('nan'))
>
> -1
> IMHO it's illogical. 'else' has completely opposite role in present
> 'try...except...' clauses. It'd be terribly missleading, not only
> for newbies.
>
More information about the Python-ideas
mailing list