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

Chris Angelico rosuav at gmail.com
Thu Feb 27 11:09:18 CET 2014


On Thu, Feb 27, 2014 at 7:44 PM, Ronald Oussoren <ronaldoussoren at mac.com> wrote:
> What about (also mentioned in the PEP)?
>
>   value = (expr except Exception try default)
>
> This seems to read nicely, although “try” is at a completely different position than it is in the equivalent try statement.
>
> I like the general idea, but like Brett I don’t like using a colon here at all.

I see your "although" clause to be quite a strong objection. In the
statement form of an if, you have:

if cond: true_suite
else: false_suite

In the expression form, you have:

true_expr if cond else false_expr

Personally, I think it's a weakness of the if-expression that they're
not in the same order (cond, true_expr, false_expr), but they're still
introduced with the same keywords. The 'if' keyword is followed by the
condition, and the 'else' keyword by the false "stuff".

Putting "try" followed by the default is confusing, because any
exception raised in the default-expr will bubble up. Stealing any
other keyword from the try/except block would make just as little
sense:

expr except Exception finally default # "finally" implies something
that always happens
expr except Exception else default # "else" implies *no* exception
expr except Exception try default # "try" indicates the initial expr,
not the default
default except Exception try expr # breaks L->R evaluation order

Left to right evaluation order is extremely important to me. I don't
know about anyone else, but since I'm the one championing the PEP,
you're going to have to show me a *really* strong incentive to reword
it to advocate something like the last one :) This is stated in the
PEP:

http://www.python.org/dev/peps/pep-0463/#alternative-proposals

Using try and except leaves the notation "mentally ambiguous" as to
which of the two outer expressions is which. It doesn't make perfect
sense either way, and I expect a lot of people would be flicking back
to the docs constantly to make sure they had it right. It's hard when
there's confusion across languages (try writing some code in REXX and
Python that uses division and modulo operators - 1234/10 -> 123.4 in
Py3 and REXX, but 1234//10 and 1234%10 have opposite meaning); it's
unnecessarily hard to have the same confusion in different places in
the same language.

ChrisA


More information about the Python-Dev mailing list