Why doesn't Python have multiline comments?

Question:

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.

Asked By: CoolGravatar

||

Answers:

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.

Answered By: Jarred McCaffrey

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.

Answered By: Abgan

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! 🙂

Answered By: Ned Batchelder

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-->.

Answered By: stalepretzel

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.

Answered By: hasen

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,

Answered By: Charlie Martin

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
Answered By: Kenan Banks
# This
# is
# a 
# multi-line
# comment

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

Answered By: recursive

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.

Answered By: Steve Losh

From The Zen of Python:

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

Answered By: Jeremy Cantrell

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.

Answered By: Kezzer

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.

Answered By: martiert

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

'''
Answered By: turvyc

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.

Answered By: kati

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.

Answered By: Jorgesys

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

  • Code | Comment with Line Comment
  • Windows or Linux: Ctrl + /
  • Mac OS: Command + /
Answered By: Craig S. Anderson

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.

Answered By: Itamar Mushkin

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!

Answered By: werner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.