[Python-Dev] PEP 463: Exception-catching expressions

Chris Angelico rosuav at gmail.com
Thu Feb 27 12:07:08 CET 2014

On Thu, Feb 27, 2014 at 9:44 PM, Ronald Oussoren <ronaldoussoren at mac.com> wrote:
>> expr except Exception try default # "try" indicates the initial expr,
>> not the default
> I didn’t parse the expression this way at all, but quite naturally parsed is as “use expr, and try using default if expr raises Exception” and not as a RTL expression.

Thing is, in the statement form, "try doing this" means "do this, and
you might get an exception, so deal with it". In the 'try default'
form, "try this" means "oops, you got an exception, so try this
instead". It's using "try" in the opposite way.

>> default except Exception try expr # breaks L->R evaluation order
>> Left to right evaluation order is extremely important to me.
> I agree with that, RTL evaluation would be pretty odd in Python.
> Really? The evaluation order you mention in above didn’t make sense to me until I tried to make sense of it.

The "default except Exception try expr" notation has "try" followed by
the thing that might raise an exception, which mimics the statement.
The "expr except Exception try default" notation evaluates from left
to right. Both make some sense, and I'd say it's on balance which is
the more likely to be expected. Imagine an overall expression where
it's ambiguous:

value = (d["foo"] except KeyError try d["spam"])

Which one do you expect to be tried first? I'd say you could poll a
bunch of moderately-experienced Python programmers and get both
answers in reasonable numbers.


More information about the Python-Dev mailing list