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

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


On Sat, Feb 22, 2014 at 3:04 PM, Chris Angelico <rosuav at gmail.com> wrote:
> 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.

I've added a couple of paragraphs to my draft PEP:

https://raw.github.com/Rosuav/ExceptExpr/master/pep-0463.txt

If someone could please commit that version to the official repo? Or I
can submit a diff against the peps repo if that would be easier.

ChrisA


More information about the Python-Dev mailing list