[Python-ideas] Allow parentheses to be used with "with" block
Chris Angelico
rosuav at gmail.com
Mon Feb 16 14:47:35 CET 2015
On Tue, Feb 17, 2015 at 12:31 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> Implicit line continuations? I think the Zen of Python has something to
> say about explicitness versus implicitness :-)
We already have implicit line continuations. Currently, you can break
a line any time the expression is incomplete - if any sort of bracket
is open. The proposal (which has come up plenty of other times) is to
deem an expression incomplete if there's a trailing binary operator.
(Incidentally, the definition depends crucially on there being no
unary operators which follow their arguments, and collide with binary
operators. But since there aren't many unary/binary collisions anyway
- the only ones I can think of are "+" and "-" - that's unlikely ever
to be an issue.) The problem isn't explicitness vs implicitness,
but...
> This would mean that the interpreter would fail to recognise certain
> syntax errors until the next line, or at all:
>
> x = 123 +
> mylist.append(None)
>
> currently reports a syntax error in the right place at compile-time:
>
> x = 123 +
> ^
> SyntaxError: invalid syntax
... the inability to adequately detect errors. So, here's an
alternative: When using this notation to continue an expression across
a line break, the following line MUST be indented further than the
current one. Again, this is what most people will do anyway, but it
means that your example would be invalid syntax still, instead of
being valid and wrong. It would be an indentation error (unexpected
indent) following a syntax error (binary operator with no second
operand).
That said, I'm not sure how this would be implemented, as I don't know
enough about the grammar. I can explain it to a human no trouble ("you
can continue an expression onto a new line by simply ending with an
operator and indenting the next line"), but it might be hard to
explain to a computer. For one thing, this version of the proposal
requires that indentation matter to the parsing of an expression,
which currently it doesn't - you can open a bracket, then put the rest
of the expression flush left, if you want to.
ChrisA
More information about the Python-ideas
mailing list