[Python-ideas] (try-except) conditional expression similar to (if-else) conditional (PEP 308)
Jae Kwon
jkwon.work at gmail.com
Fri Aug 7 10:03:02 CEST 2009
How about making this work?
foo = try: float(string) except SomethingError, e: float(NAN)
This would imply that try blocks have a value. Maybe even allow this:
foo = try:
float(string)
except Something Error, e:
float(NAN)
Sure, it introduces a couple new grammar tweaks, but it's easy to
understand and is backwards compatible.
- Jae
On Aug 7, 2009, at 12:21 AM, Mike Meyer wrote:
> On Fri, 07 Aug 2009 08:30:14 +0200
> Georg Brandl <g.brandl at gmx.net> wrote:
>
>> MRAB schrieb:
>>> 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')
>>
>> Don't over-complicate. This proposal has a slim chance of success
>> as it
>> is; piling on top isn't going to help.
>
> Assuming you avoid the ':' version, shouldn't multiple excepts fall
> out of the grammar naturally?
>
> x = ((1.0 / float(string)
> except float('nan') if ValueError)
> except float('inf') if ZeroDivisionError)
>
> parses as "expr1 except float('inf') if ValueError", where expr1 is
> "(1.0 / float(string) except float('nan') if ValueError)".
>
> Yes, it's as ugly as nesting if/else expressions, but those work, so
> these should.
>
> FWIW, I'm +.5 on this. I don't see a lot of use for it, but can see
> that where it is useful, it's easier to read than a try:/except:.
>
> <mike
> --
> Mike Meyer <mwm at mired.org> http://www.mired.org/consulting.html
> Independent Network/Unix/Perforce consultant, email for more
> information.
>
> O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
More information about the Python-ideas
mailing list