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

Eric V. Smith eric at trueblade.com
Fri Feb 21 23:26:54 CET 2014



On 2/21/2014 5:06 PM, Greg Ewing wrote:
> Nick Coghlan wrote:
>> On 21 February 2014 13:15, Chris Angelico <rosuav at gmail.com> wrote:
>>
>>> Generator expressions require parentheses, unless they would be
>>> strictly redundant.  Ambiguities with except expressions could be
>>> resolved in the same way, forcing nested except-in-except trees to be
>>> correctly parenthesized 
>>
>> I'd like to make the case that the PEP should adopt this as its
>> default position.
> 
> I generally agree, but I'd like to point out that this
> doesn't necessarily mean making the parenthesizing rules as
> strict as they are for generator expressions.
> 
> The starting point for genexps is that the parens are part of
> the syntax, the same way that square brackets are part of
> the syntax of a list comprehension; we only allow them to
> be omitted in very special circumstances.
> 
> On the other hand, I don't think there's any harm in allowing
> an except expression to stand on its own when there is no
> risk of ambiguity, e.g.
> 
>    foo = things[i] except IndexError: None

I agree that it would be a shame to disallow this simple usage. I'd like
to leave this like any other expression: add parens if they add clarity
or if they are required for the precedence you'd prefer. Personally, I'd
probably always use parens unless it was a simple assignment (as above),
or an argument to a function call. But I think it's a style issue.

Eric.

> should be allowed, just as we allow
> 
>    x = a if b else c
> 
> and don't require
> 
>    x = (a if b else c)
> 


More information about the Python-Dev mailing list