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

Chris Angelico rosuav at gmail.com
Fri Feb 21 15:59:15 CET 2014


On Sat, Feb 22, 2014 at 12:53 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 21 February 2014 22:42, Chris Angelico <rosuav at gmail.com> wrote:
>> People can already write:
>>
>> if (x if y else z):
>>
>> without the parens, and it works. Readability suffers when the same
>> keyword is used twice (here "if" rather than the colon, but same
>> difference), yet the parens are considered optional. Python is a
>> language that, by and large, lacks syntactic salt; style guides are
>> free to stipulate more, but the language doesn't make demands. I would
>> strongly *recommend* using parens in all the cases you've shown,
>> especially lambda:
>>
>>>    lambda x: calculate(x) except Exception: None
>>>    lambda x: (calculate(x) except Exception: None)
>>
>> as it would otherwise depend on operator precedence; but mandating
>> them feels to me like demanding readability.
>
> Right, that's why my main motivation for this suggestion is the one
> relating to keeping future options open. If the parentheses are
> optional, than adding multiple except clauses latter isn't possible,
> since this would already be valid, but mean something different:
>
>     expr except Exception1: default1 except Exception2: default2
>
> The deferral currently has this snippet:
>
> """In order to ensure compatibility with future versions, ensure that
> any consecutive except operators are parenthesized to guarantee the
> interpretation you expect."""
>
> That's not a reasonable expectation - either the parentheses have to
> be mandatory as part of the deferral, or else multiple except clause
> support needs to be listed as rejected rather than deferred.

I've spent the better part of the last hour debating this in my head.
It's basically a question of simplicity versus future flexibility:
either keep the syntax clean and deny the multiple-except-clause
option, or mandate the parens and permit it. The first option has, in
my own head, the stronger case - this is designed for simplicity, and
it wouldn't be that big a deal to completely reject multiple except
clauses and simply require that the


More information about the Python-Dev mailing list