[Python-ideas] Allow parentheses to be used with "with" block

Neil Girdhar mistersheik at gmail.com
Tue Feb 17 00:22:35 CET 2015


On Mon, Feb 16, 2015 at 6:17 PM, Andrew Barnert <abarnert at yahoo.com> wrote:

> On Feb 16, 2015, at 14:18, Neil Girdhar <mistersheik at gmail.com> wrote:
>
> > I think the most consistent way to do this if we would do it is to
> support parentheses around the "with statement".  We now know that though
> it's not "trivial", it's not too hard to implement it in the way that
> Andrew suggests.
>
> Hold on, what did I suggest?
>

I thought we could just change the rules:

with_stmt: 'with' exprlist  ':' suite
exprlist: (expr ['as' expr] |star_expr) (',' (expr ['as' expr]|star_expr))*
[',']

And then preclude the use of "as" in all the other exprlist contexts (among
other things) in the validator.


> Sure, it's not too hard to add an extragrammatical rule to the language
> and a simply hacky workaround to the CPython parser. And in principle it's
> not hard to rewrite Python's grammar unambiguously in a more powerful
> metalanguage that could be parsed with a more powerful parser. But I'm
> definitely not suggesting either.
>
> Just because it's good enough for C doesn't mean it's good enough for
> Python. (And it's really not even good enough for C. Most modern C-family
> parsers gave up on trying to pile hacks onto an LALR parser and instead use
> custom recursive-descent parsers that are nothing but special cases, or
> rewrite the grammar in GLR or packrat terms.)
>
> The fact that Python is simply LL(1)-parseable is a good thing. You can
> parse it in your head, predict the ASTs and read them (well, with a bit of
> pretty-printing indentation). If you can't find a way to add the parens
> that preserves this property, I think the cost far outweighs the benefit.
>
>
It would still be LL(1).


> (The lexer-based implicit continuation is theoretically not as bad, but
> practically even worse; just extending the
> unbalanced-brackets/inexplicable-syntax-error problem to places without
> unbalanced brackets is enough reason not to do it.)
>

I'm not sure what this means?


>
> > I'd consider doing it if there were an approved PEP and a guaranteed
> fast review.
> >
> > Best,
> >
> > Neil
> > _______________________________________________
> > Python-ideas mailing list
> > Python-ideas at python.org
> > https://mail.python.org/mailman/listinfo/python-ideas
> > Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150216/db4c01d2/attachment-0001.html>


More information about the Python-ideas mailing list