tags:

views:

4769

answers:

13

Ok, I'm aware that triple-quotes strings can serve as multiline comments e.g.

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

Thanks

+15  A: 

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.

Jarred McCaffrey
+11  A: 

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.

Abgan
+45  A: 

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

Ned Batchelder
+2  A: 

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

stalepretzel
A: 

This is just a guess .. but

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

hasen j
A: 

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,

Charlie Martin
A: 

As a side note, most decent code editors and IDEs have a "comment selected lines" for adding comment signs before each line that's selected.

Soviut
+8  A: 

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
Triptych
+3  A: 
# This
# is
# a 
# multi-line
# comment

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

recursive
+12  A: 

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.

Steve Losh
The nicest thing about this answer is how it is handled by SO's syntax highlighter.
Cirno de Bergerac
I didn't even notice... I rest my case. :)
Steve Losh
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.
Nathan Adams
No escape characters would help here. If the */ in the string is escaped to allow the comment, it would show up in the string when it's **not** commented. Otherwise it breaks the comment. If you want to add even more escapes to strings to allow for comments, I think that would add unnecessary complexity to the language's string syntax.
Steve Losh
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.
Nathan Adams
+1  A: 

From The Zen of Python:

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

Jeremy Cantrell
A: 

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.

Kezzer
+3  A: 

Besides, multiline comments are a bitch. Sorry to say, but regardless of the language, I don't use them for anything else then debugging purposes. Say you have a 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 realy irritating.

martiert