Comments in continuation lines
Asked Answered
S

4

25

Say I have a multiline command:

if 2>1 \
 and 3>2:
    print True

In an if block, I can add a comment next to one of the conditions by using parentheses to wrap the lines:

if (2>1 #my comment
 and 3>2):
    print True

And, in fact, it is aligned with the recommened way of doing this by PEP 8 guideline:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

However, sometimes you need to use continuations. For example, long, multiple with-statements cannot use implicit continuation. Then, how can I add a comment next to a specific line? This does not work:

with open('a') as f1, #my comment\
 open('b') as f2:
    print True

More generally, is there a generic way to add a comment next to a specific continuation line?

Solitude answered 5/5, 2015 at 10:26 Comment(2)
Spyder tells me "invalid sytax" when I put your code in, and "unexpected character after line continuation character" when I put a `` before the comment, so I'm guessing no, you can't do line comments with line continuation. I'm guessing that it has to do with trying to splice a comment into a statement, i.e. x = 2 + #comment# 3Duckpin
It's a pity that this can't be done. I found it very useful in Matlab. I'm slowly building up competency in Python, but missing of Matlab.Joyann
C
25

You cannot. Find some extracts from Python reference manual (3.4):

A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line.

A line ending in a backslash cannot carry a comment

A comment signifies the end of the logical line unless the implicit line joining rules are invoked

Implicit line joining : Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes

Implicitly continued lines can carry comments

So the reference manual explicitly disallows to add a comment in an explicit continuation line.

Cassy answered 5/5, 2015 at 12:22 Comment(0)
H
5

You can't have comments and backslash for line continuation on the same line. You need to use some other strategy.

The most basic would be to adjust the comment text to place it e.g. before the relevant section. You could also document your intentions without comments at all by refactoring the code returning the context into a function or method with a descriptive name.

Harold answered 5/5, 2015 at 11:23 Comment(1)
Interesting!. I am accepting the other answer, since it provides some references. However, this one I will also use. Many thanks!Solitude
A
2

I don't see any solution except nesting the with:

with open('a.txt', 'w') as f1: #comment1
    with open('b.txt', 'w') as f2: #comment2
        print True
Abad answered 5/5, 2015 at 10:57 Comment(2)
Obviously the with-statement is not "multiple" anymore.Abad
This is a clever way to prevent the problem from happening. However, I am still curious about how to do this using continuation lines.Solitude
M
0

You cannot combine end-of-line comment (#) and line continuation (\) on the same line.

I am not recommending this. -- However, sometimes you can masquerade your comment as a string:

with open(('a', '# COMMENT THIS')[0]) as f1, \
     open(('b', '# COMMENT THAT')[0]) as f2:
    print(f1, f2)
Malena answered 18/2, 2021 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.