81

This doesn't work:

something = \
    line_of_code * \    #  Comment
    another_line_of_code * \    #  Comment
    and_another_one * \         #  Comment
    etc

Neither does this:

something = \
    #  Comment \
    line_of_code * \
    #  Comment \
    another_line_of_code * ...

Neither does this:

something = \
    ''' Comment ''' \
    line_of_code * \
    ''' Comment ''' \
    another_line_of_code * ...

Is there a way to make comments in the code broken into multiple lines?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
MarcinKonowalczyk
  • 2,577
  • 4
  • 20
  • 26

2 Answers2

86

Do it like that:

a, b, c, d = range(1, 5)

result = (
    # First is 1
    a *
    # Then goes 2, result is 2 now
    b *
    # And then 3, result is 6
    c *
    # And 4, result should be 24
    d
)

Actually, according to PEP8 parentheses are preferred over slashes, when breaking something into multiple lines:

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.

In your case it also allows to put comments.

Here is a proof, that it works: http://ideone.com/FlccUJ

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 1
    Also, you can cehck this out: http://www.python.org/dev/peps/pep-0008/#maximum-line-length – Yotam Jul 13 '13 at 14:11
  • @Yotam: Actually, see the link attached to "_according to PEP8_" text in my answer. Also, the quote from my answer goes directly from what you have pasted here. – Tadeck Jul 13 '13 at 14:13
  • 11
    What should we do to split over the dots in this case ? `obj.method1(args1).method2(args2).method3(args3)` – iago-lito Nov 20 '15 at 09:14
  • Not sure of an in-line way, but you can do `a=obj.method1(args) # comm1` & `b=obj.method2(args2) # comm2` etc. – Demis Jan 06 '16 at 17:40
  • 3
    @lago-lito: In such case I would split it using parentheses, too. If you want to comment each segment, then obviously you can still do this as per PEP8: `obj.method1(` \n `# Important method 1` \n `args1` \n `).method2(` \n `# Importand other method` \n `args2` \n `).method3(` \n `# Method that needs to be called last` \n `args3` \n `)`. Maybe it looks too expressive, but hey - it is you who wants to comment every method in the chain ;) – Tadeck Jan 06 '16 at 22:36
  • @iago-lito, as long as the expression is contained within outer parentheses, there can be white space (new lines and/or spaces, I didn't test tabs) before or after periods and inner parentheses, so the splits can basically go wherever you want. – Todd Jun 15 '21 at 23:57
1

Not sure what you are trying to do is supported by python. Read PEP8 section about inline comments. Putting comments in the middle of line continuations is "ugly" and probably confusing.

Python way is with # on every line if you want to comments something or for inline comments everything after # is ignored.

If you really want to comment a multiline statement that is really necessary put it before or after it.

a, b, c, d = range(1, 5)
# a is ..., b is ...
# c is ..., d is ...
result = (a, b, c, d)

definately don't want to get into an argument about style, but just because you can do something doesn't mean that it is clear. Inline comments are great for clarifyling short lines of code that just need a short pointer.

Joop
  • 7,840
  • 9
  • 43
  • 58
  • 2
    Well, the referenced section in PEP8 says nothing about comments in line continuations. It says that inline comments are distracting, if obvious, but are sometimes useful. In this case I am assuming they _are_ useful. Also it is possible to comment multiline statements (see [my answer](http://stackoverflow.com/a/17630918/548696)). And the standard is to put block comments _before_ thing that they are commenting ([source](http://www.python.org/dev/peps/pep-0008/#block-comments)) - of course this is about _block_ comments, not _inline_ comments (which obviously have to be after commented part). – Tadeck Jul 13 '13 at 14:20