288

OK, I'm aware that triple-quotes strings can serve as multiline comments. For example,

"""Hello, I am a 
   multiline comment"""

and

'''Hello, I am a 
   multiline comment'''

But technically speaking these are strings, correct?

I've googled and read the Python style guide, but I was unable to find a technical answer to why there is no formal implementation of multiline, /* */ type of comments. I have no problem using triple quotes, but I am a little curious as to what led to this design decision.

9
  • 8
    If you can do it as a string why add more ways? Commented Dec 29, 2008 at 5:22
  • 15
    Just wanted to add, that it fails if what you are trying to comment happens to also have comments/multi-line strings. And that of course, is why we need them. Commented Apr 2, 2012 at 14:28
  • 60
    @S.Lott I think it's a useful question. In order to understand why Python is good, it's important to understand the design decisions that were made (and on-going decisions that are still being made). The question isn't argumentative or combative; it's curious. No need to be so harsh about curiosity. Commented Jun 20, 2012 at 13:01
  • 9
    If you need a multi line comment for cod just if False: the code Commented Mar 11, 2015 at 13:39
  • 10
    @Brody Because strings are processed. Comments are ignored. There are problems with using strings as comments. Just look around :) Commented Jul 18, 2015 at 5:39

19 Answers 19

290

I doubt you'll get a better answer than, "Guido didn't feel the need for multi-line comments".

Guido has tweeted about this:

Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)

Sign up to request clarification or add additional context in comments.

11 Comments

See Guido's tweet on this.
one disadvantage of mix multi-line string and block comments is IDE has no idea what you want thus can't show comment in different style as needed.
It also makes it impossible to comment out code with multi-line strings (and can lead to indentation errors if you're not careful). Ew!
I have worked in a lot of fields where if your code contains commented out code then your code is rejected and you may even find yourself invited to update your CV. Either remove the code that is not needed, not a problem if the code is under version control, or use if False: before the code that needs to be disabled.
@SteveBarnes agree that big blocks of commented-out code in production are bad. But I don't understand why if False is better. It accomplishes exactly the same thing, while being less clear (since it isn't as obvious at a glance that the block of code has been disabled).
|
62

Multi-line comments are easily breakable. What if you have the following in a simple calculator program?

operation = ''
print("Pick an operation:  +-*/")
# Get user input here

Try to comment that with a multi-line comment:

/*
operation = ''
print("Pick an operation:  +-*/")
# Get user input here
*/

Oops, your string contains the end comment delimiter.

14 Comments

The nicest thing about this answer is how it is handled by SO's syntax highlighter.
This is one of the many reasons why we have escape characters, I don't see that as a good reason to NOT have support for multi-line comments.
I don't understand your logic - perhaps my comment wasn't clear enough. If we used \ as an escape character: print("Pick an operation: +-*\/") "*/" no longer denotes a ending comment block as literally / will be printed. Go ahead and test this in C++. In fact SO's syntax highlighter will show that is valid. This is not a complex subject, it has existed for years in other languages. I would ask that you update your post to include the use of escape characters to show that you CAN use "*/" in your code.
what if your code contains ''' . oops your code contains the end comment delimiter
Multi-line comments aren't inherently breakable; it's just that most implementations of them are (including Python's). The obvious way to do multi-line comments in Python, to my mind, is to just let me start a comment block with #: and use indentation to show when the comment's ended. It's clean, consistent, and handles nesting perfectly.
|
41

Triple-quoted text should NOT be considered multi-line comments; by convention, they are docstrings. They should describe what your code does and how to use it, but not for things like commenting out blocks of code.

According to Guido, multiline comments in Python are just contiguous single-line comments (search for "block comments").

To comment blocks of code, I sometimes use the following pattern:

if False:
    # A bunch of code

5 Comments

Seems like Guido chanded his mind since then.
regarding the "if false:" solution, the thing is that in python as it works with tabs, you'd have to tab in all the code under the "if False:". And untab the chunk afterwards. So you'd have to be pretty nifty with your text editor.
if you use a decent editor, that should be the same amount of time as */
@barlop yup - learn your editors! This is typically achievable in under a second in vim with V}>>
Multi-line/triple-quoted strings don't have to be docstrings, and vice versa. A docstring is "a string literal that occurs as the first statement in a module, function, class, or method definition", whether or not it's multi-line. Unused (unassigned or not otherwise used in a statement/expression) literals elsewhere in your code, string or otherwise, are discarded at compile time.
32

This likely goes back to the core concept that there should be one obvious way to do a task. Additional comment styles add unnecessary complications and could decrease readability.

5 Comments

That's the issue, I believe: Using a string as a comment is not obvious and violates the "one way to do a task" principle, since there are two ways to do comments: strings and #.
But its not significantly different than what you have in C-based languages: /* vs // , so i don't see how its significantly worse.
// , Consider WHY someone would want a multi-line comment. Good reasons: ... I can't really think of any beyond "I don't have to type as many of these # doohickeys" and "I need to display this particular comment in a very precise way, and that precise way doesn't allow for preceding #." Say someone wants to do an ASCII diagram, or put some reference javascript code to be copied and pasted if a specific problem comes up. The one obvious way to do a task, here, doesn't cover the edge cases of that task. I agree, though, that additional comment styles are BAD.
"I don't have to type as many of these # doohickeys". That is precisely why pretty much all languages have block comments (/* ..*/). Believe it or not, but I like to document what my code does: the inputs, the outputs, the algorithms used, the parameters ... That is a lot of text that also gets modified. The restriction to only single-line comments is just plain ridiculous. Note that I do NOT advocate the approach for commenting out code - although that is often handy when trying alternate approaches, as long as the well known possible side effects are understood.
The other thing I resent about python is that it is essentially a one-man designed language. Whatever Guido says is the truth ... So we have all those odd incompatibilities between language versions. Why ? Because Guido said so ...
12

Well, the triple-quotes are used as multiline comments in docstrings. And # comments are used as inline comments and people get use to it.

Most of script languages don't have multiline comments either. Maybe that's the cause?

See PEP 0008, section Comments

And see if your Python editor offers some keyboard shortcut for block commenting. Emacs supports it, as well as Eclipse, presumably most of decent IDEs does.

Comments

9

From The Zen of Python:

There should be one-- and preferably only one --obvious way to do it.

2 Comments

Yet Python doesn't follow this at all.. 4 different ways to exit a program for example. Many other examples.
That's a dream of the old times... Life it's harder than just quoting your grandpa, and we need things that make our lives easier. Just ending this discussion would be justification enough for multiline comments.
7

To comment out a block of code in the Pycharm IDE:

  • Code | Comment with Line Comment
  • Windows or Linux: Ctrl + /
  • Mac OS: Command + /

Comments

5

Personally my comment style in say Java is like

/*
 * My multi-line comment in Java
 */

So having single-line only comments isn't such a bad thing if your style is typical to the preceding example because in comparison you'd have

#
# My multi-line comment in Python
#

VB.NET is also a language with single-line only commenting, and personally I find it annoying as comments end up looking less likes comments and more like some kind of quote

'
' This is a VB.NET example
'

Single-line-only comments end up having less character-usage than multi-line comments, and are less likely to be escaped by some dodgy characters in a regex statement perhaps? I'd tend to agree with Ned though.

Comments

4

For anyone else looking for multi-line comments in Python - using the triple quote format can have some problematic consequences, as I've just learned the hard way. Consider this:

this_dict = {
    'name': 'Bob',
    
"""
This is a multiline comment in the middle of a dictionary
"""
    
    'species': 'Cat'
}

The multi-line comment will be tucked into the next string, messing up the 'species' key. Better to just use # for comments.

[EDITED: of course, for the intended use of triple quotes, which is for docstrings, this is not a consideration]

2 Comments

This is the reason why we need multiple line syntax. Why doesn't Guido feel the need to do that?
@Ooker a normal IDE can just comment out multiple line in one click, as other have pointed out (like stackoverflow.com/a/29478739/8874696), so the whole thing is unnecessary
3
# This
# is
# a 
# multi-line
# comment

Use comment block or search and replace (s/^/#/g) in your editor to achieve this.

1 Comment

Editing macros aren't well known, which is why /* */ is useful: it is meant for spanning multiple lines.
3

I solved this by downloading a macro for my text editor (TextPad) that lets me highlight lines and it then inserts # at the first of each line. A similar macro removes the #'s. Some may ask why multiline is necessary but it comes in handy when you're trying to "turn off" a block of code for debugging purposes.

Comments

1

There should only be one way to do a thing, is contradicted by the usage of multiline strings and single line strings or switch/case and if, different form of loops.

Multiline comments are a pretty common feature and lets face it the multiline string comment is a hack with negative sideffects! I have seen lots of code doing the multiline comment trick and even editors use it.

But I guess every language has its quirks where the devs insist on never fixing it. I know such quirks from the java side as well, which have been open since the late 90s, never to be fixed!

Comments

0

Because the # convention is a common one, and there really isn't anything you can do with a multiline comment that you can't with a #-sign comment. It's a historical accident, like the ancestry of /* ... */ comments going back to PL/I,

Comments

0

Assume that they were just considered unnecessary. Since it's so easy to just type #a comment, multiline comments can just consist of many single line comments.

For HTML, on the other hand, there's more of a need for multiliners. It's harder to keep typing <!--comments like this-->.

2 Comments

this is not the point - there are obvious use cases for both single line and multi line comments. I have used them both extensively in other languages (though I know python purists don't care about other languages). ;)
try doing this with 200 lines of code, that you have to take out, put back in, then take out again. Typing 200 initial #'s gets old real fast.
0

This is just a guess .. but

Because they are strings, they have some semantic value (the compiler doesn't get rid of them), therefore it makes sense for them to be used as docstrings. They actually become part of the AST, so extracting documentation becomes easier.

Comments

0

Besides, multiline comments are a bitch. Sorry to say, but regardless of the language, I don't use them for anything else than debugging purposes. Say you have code like this:

void someFunction()
{
    Something
    /*Some comments*/
    Something else
}

Then you find out that there is something in your code you can't fix with the debugger, so you start manually debugging it by commenting out smaller and smaller chuncks of code with theese multiline comments. This would then give the function:

void someFunction()
{ /*
    Something
   /* Comments */
   Something more*/
}

This is really irritating.

5 Comments

uh great, but Python doesn't have /*-style comments.
Right, since python doesn't have real multiline comments it was kind of hard giving examples in python.
I personally don't understand the problem. Just delete the extra */. Or use // to comment out single lines if you need to be precise.
There are several languages (many of them functional for whatever reason) which allow nested comments. Search for "nested" in rosettacode.org/wiki/Comments for examples.
well yeah it would be irritating to put a multi-line comment in a multi-line comment. And while I only remember a bit of my program at a time, I at least remember which part of my program I am looking at and so which I have commented out. But if you can't even remember that, then, you can use the fact that some IDEs make italic what is a comment. Anyhow obviously for such a tiny function, you may as well use single line comments. But if commenting out a big chunk of program, you need a multi-line comment really. or a text editor with that feature.
0

Multiline comments using IDLE on:

  • Mac OS X, after code selection, comment a block of code with Ctrl+3 and uncomment using Ctrl+4.

  • Windows, after code selection, comment a block of code with Ctrl+Alt+3 and uncomment using Ctrl+At+4.

1 Comment

Now, for windows, the shortcuts is Alt +3 to comment, and Alt + 4 to uncomment
0

I'm against:

Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)

When I use block comments to comment out arguments,

someFunction(arg1, /* arg2 */) // properly commented out

python doesn't work this way:

someFunction(arg1, """arg2""") # still an invalid string argument

Comments

-1

I remember reading about one guy who would put his multi-line comments into a triple-quoted variable:

x = '''
This is my
super-long mega-comment.
Wow there are a lot of lines
going on here!
'''

This does take up a bit of memory, but it gives you multi-line comment functionality, and plus most editors will highlight the syntax for you :)

It's also easy to comment out code by simply wrapping it with

x = '''

and

'''

1 Comment

remove the x = and it doesn't take up any memory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.