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

Thomas Wouters thomas at python.org
Fri Feb 28 11:59:19 CET 2014


On Fri, Feb 28, 2014 at 1:30 AM, Glenn Linderman <v+python at g.nevcal.com>wrote:

>
>     value = expr except (
>         Exception1: default1,
>         Exception2: default2,
>         Exception3: default3,
>    )
>
> except that to get the pairing aspect of some parameters for a function call, you use = instead of :, and instead of a named function it has an expression and a keyword.
>
> [ Cue people suggesting the use of '=' or '=>' or '->' instead of ':' ]


> Not being an expert parser generator, I don't know if the () could
>     be made optional if there is only one exception-list, but that would
>     also remove one of the benefits some folks might perceive with using
>     this syntax, and would also make the analogy with function call
>     syntax a little less comparable.
>
> I believe it's possible, but it's probably tricky: the parser has to
consider two or three cases:

1.  expr1 except expr2: expr4
2.  expr1 except (expr2: expr4)
3.  expr1 except (expr2, expr3): expr4
4.  expr1 except ((expr2, expr3): expr4)

(for simplicity I use 'expr' here, which is entirely the wrong thing to do
in terms of CPython's grammar: there's different types of expressions for
different situations. #2 and #4 are actually automatically derived from #1
and #3 by 'expr' including tuples and parenthesized expressions.) CPython's
parser is a LL(1) parser, which means it can look ahead 1 character to
choose between alternatives (and that's not going to change, Guido likes it
this way -- and so I do, personally :). Looking at #2 and #3 you can't tell
which alternative to use until you see the ':' or ',' after 'expr2', which
is too late. The only way to handle that, I think, is to include all
possible alternatives in the grammar for the 'except' statement,
duplicating logic and definitions from a lot of places. We already do this
kind of thing to deal with other potentially-ambiguous situations (to
handle dicts and sets and dict comprehensions and set comprehensions, for
example) but this would be even more duplication.

It would be easier if we weren't talking about '(' or any other token that
can be part of a normal expression ;-P

[ Cue people suggesting the use of 'expr1 except < expr2: expr4 >'... ]

-- 
Thomas Wouters <thomas at python.org>

Hi! I'm an email virus! Think twice before sending your email to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140228/775dd578/attachment.html>


More information about the Python-Dev mailing list