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

Andrew Barnert abarnert at yahoo.com
Tue Feb 17 00:17:39 CET 2015


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?

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.

(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'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/


More information about the Python-ideas mailing list