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

Chris Angelico rosuav at gmail.com
Sat Feb 22 05:04:35 CET 2014


On Sat, Feb 22, 2014 at 1:07 PM, Victor Stinner
<victor.stinner at gmail.com> wrote:
> At the first read, I'm unable to understand this long expression. At
> the second read, I'm still unable to see which instruction will be
> executed first: lvl1[key] or lvl2[key]?
>
> The advantage of the current syntax is that the control flow is
> obvious, from the top to the bottom:
>
> # start
> try:
>     x = lvl1[key]   # first instruction
> except KeyError:
>     try:
>         x = lvl2[key]
>     except KeyError:
>         x = f(key)   # latest instruction
> # end

That's why I'm strongly in favour of syntax variants that have
evaluation order be equally obvious: left to right. Its notation may
be uglier, but C's ternary operator does get this right, where
Python's executes from the inside out. It's not a big deal when most
of it is constants, but it can help a lot when the expressions nest.

ChrisA


More information about the Python-Dev mailing list