Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in continuation lines

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?

like image 814
fedorqui 'SO stop harming' Avatar asked May 05 '15 10:05

fedorqui 'SO stop harming'


People also ask

How do you continue comments in Python?

Unlike most other programming languages, Python has no built-in syntax for creating multi-line comments. Above, we placed the # symbol on each line to continue writing our comment.

What are continuation lines?

Any sentence, entry, clause, or phrase that requires more than one line can be continued in Area B of the next line that is neither a comment line nor a blank line. The line being continued is a continued line ; the succeeding lines are continuation lines .

What is the line continuation character and what does it do?

The line continuation character in IPL and JavaScript is the backslash (\). You use this character to indicate that the code on a subsequent line is a continuation of the current statement. The line continuation character helps you format your policies so that they are easier to read and maintain.


2 Answers

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.

like image 120
Serge Ballesta Avatar answered Oct 13 '22 01:10

Serge Ballesta


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.

like image 23
Dag Høidahl Avatar answered Oct 13 '22 00:10

Dag Høidahl