[Python-ideas] Allowing comments after line continuations
Terry Jan Reedy
tjreedy at udel.edu
Thu May 16 23:55:48 CEST 2013
On 5/16/2013 2:41 PM, Bruce Leban wrote:
> The \ line continuation does not allow comments yet statements that span
> multiple lines may need internal comments.
In a string, \ escapes the immediate next character. This idea of \ at
the *end* of a line, just before the newline character, is that it
escapes the newline, *just as is does within a string*.
>>> 'abd\
ed'
'abded'
>>> 1 +\
2
3
In both cases, one would typically have a syntax error without the \.
>>> 'abc
SyntaxError: EOL while scanning string literal
>>> a +
SyntaxError: invalid syntax
I think changing the correspondance is a bad idea. A code line is an
string (but not an str object) that is fed to the interpreter.
Besides which, end of line \ is generally discouraged and nearly always
not needed as openers other that ' and ", namely {, [, and (, enable
line continuation without \. So if you want comments on each line, add
parens.
> with a as x, \ # comment here may be useful
> b as y, \ # or here
> c as z: \ # or here
> pass
This is one of the very few cases where bracketing is not allowed. I do
not remember why. The PEP might say why not. See ho
>>> from itertools import (chain, # very helpful
count) # also helpful
Here () is allowed precisely for line continuation.
> x = y + # syntax error
> z
x = (1 + # not a syntax error
2) # and more comment
> Two reasons for requiring a space after the backslash:
>
> (1) make the backslash more likely to stand out visually (and we can't
> require a space before it)
I think it stands out better by itself at the end.
Terry Jan Reedy
More information about the Python-ideas
mailing list