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

Glenn Linderman v+python at g.nevcal.com
Fri Feb 28 08:38:57 CET 2014

On 2/27/2014 7:46 PM, Chris Angelico wrote:
> On Fri, Feb 28, 2014 at 1:12 PM, Glenn Linderman <v+python at g.nevcal.com> wrote:
>> Yes.  But the point is really the location of the (), sorry if my "nice
>> extension" comment is throwing you off that track.
> Ah! I see.
> We touched on this syntax on -ideas, but at the time, the proposed
> syntax didn't have parens around the outside. Either location will
> solve most of the same problems (like precedence/associativity versus
> multiple except clauses), but shrinking down to just the exception
> list and default value makes it look very different. It looks like
> what's inside should be an expression, which it isn't. What's the
> advantage of this form? What's its key advantage over the
> parens-around-the-whole-thing form?

Key advantage to me is that if a function call or other expression may 
produce multiple exceptions, the syntax doesn't require repeating the 
"except" over and over.  By not repeating the except over and over, 
there is less ambiguity about what expression the Exception-lists apply 
to, when there is more than one Exception list in the expression.  
Whereas the current PEP syntax has ambiguity regarding how to interpret  
a-expr except except-list-b: b-expr except except-list-c: c-expr (does 
the 2nd except apply to a-expr or b-expr?), without parentheses, and, as 
far as I am concerned, even with the parentheses, this syntax makes it 
very clear that each of the Exception-lists apply to a-expr.

Key advantage to others may be that because the : is within the () [and 
the leading ( is quite nearby, making it obvious], it is less likely to 
be considered a statement boundary, and more easily explained as a 
special type of list syntax... not _really_ a list, because it is really 
code to be executed somewhat sequentially rather than data, and lists 
don't have : ... and not _really_ a dict constant, which does have :, 
because the Exception is not _really_ a key, but the syntax can draw on 
analogies with the dict constant syntax which will help people remember 
it, and even sort of understand that there is a pair-wise relationship 
between the Exception-list and the expression after the :, without 
repeating the except over and over.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140227/3c41a088/attachment.html>

More information about the Python-Dev mailing list