41

Why didn't python just use the traditional style of comments like C/C++/Java uses:

/**
 * Comment lines 
 * More comment lines
 */

// line comments
// line comments
//

Is there a specific reason for this or is it just arbitrary?

6
  • 3
    What? Python uses # comments, which are common practice in lots of languages. Commented Aug 14, 2011 at 14:47
  • 2
    I meant the multi-line comments which start with """ I believe...
    – mmtauqir
    Commented Aug 14, 2011 at 14:50
  • I know # is used in other languages (I know bash uses it).
    – mmtauqir
    Commented Aug 14, 2011 at 14:50
  • Those aren't comments, they're docstrings. Commented Aug 14, 2011 at 14:51
  • 2
    They aren't docstrings. They're just commonly used for docstrings so the string is easier to read.
    – jamessan
    Commented Aug 14, 2011 at 15:05

5 Answers 5

106

Python doesn't use triple quotation marks for comments. Comments use the hash (a.k.a. pound) character:

# this is a comment

The triple quote thing is a doc string, and, unlike a comment, is actually available as a real string to the program:

>>> def bla():
...     """Print the answer"""
...     print 42
...
>>> bla.__doc__
'Print the answer'
>>> help(bla)
Help on function bla in module __main__:

bla()
    Print the answer

It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention (and has the advantage of being multiline).

5
  • 11
    "It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention." Huh, neat. I had no idea about that!
    – akbiggs
    Commented Aug 14, 2011 at 14:52
  • OIC now...I should have done some more research myself before asking this question...thanks though!
    – mmtauqir
    Commented Aug 14, 2011 at 14:52
  • 3
    Excellent answer. I'd complement that it is usual to use strings as quick and dirty multiline comments too, when you want to comment out some lines for debugging purposes.
    – brandizzi
    Commented Aug 14, 2011 at 15:33
  • 2
    Great example code, but this answer lacks one detail I was looking for, which @eli-collins mentions in his answer below: "bare strings which follow immediately after a def Foo(), class Foo(), or the start of a module, are treated as string containing documentation for that object, and stored in the __doc__ attribute of the object". Commented Sep 19, 2018 at 16:13
  • 1
    To clarify what @RemingtonSteed mentioned: the string literal is only interpreted as a docstring when it is the first statement in "a module, function, class, or method definition" (python.org). So you can use it like a comment in all other circumstances, and it is not accessible as a runtime object attribute. Commented Aug 30, 2020 at 17:45
54

A number of the answers got many of the points, but don't give the complete view of how things work. To summarize...

# comment is how Python does actual comments (similar to bash, and some other languages). Python only has "to the end of the line" comments, it has no explicit multi-line comment wrapper (as opposed to javascript's /* .. */). Most Python IDEs let you select-and-comment a block at a time, this is how many people handle that situation.

Then there are normal single-line python strings: They can use ' or " quotation marks (eg 'foo' "bar"). The main limitation with these is that they don't wrap across multiple lines. That's what multiline-strings are for: These are strings surrounded by triple single or double quotes (''' or """) and are terminated only when a matching unescaped terminator is found. They can go on for as many lines as needed, and include all intervening whitespace.

Either of these two string types define a completely normal string object. They can be assigned a variable name, have operators applied to them, etc. Once parsed, there are no differences between any of the formats. However, there are two special cases based on where the string is and how it's used...

First, if a string just written down, with no additional operations applied, and not assigned to a variable, what happens to it? When the code executes, the bare string is basically discarded. So people have found it convenient to comment out large bits of python code using multi-line strings (providing you escape any internal multi-line strings). This isn't that common, or semantically correct, but it is allowed.

The second use is that any such bare strings which follow immediately after a def Foo(), class Foo(), or the start of a module, are treated as string containing documentation for that object, and stored in the __doc__ attribute of the object. This is the most common case where strings can seem like they are a "comment". The difference is that they are performing an active role as part of the parsed code, being stored in __doc__... and unlike a comment, they can be read at runtime.

3
  • "can be stored in a variable" There are no accessible variables in Python in which a value can be put in and pulled out. There are only objects.
    – eyquem
    Commented Aug 14, 2011 at 22:24
  • 4
    True that. The terminology was perhaps a little too informal, but I was trying to keep my answer from going on for too long. "can have a reference to itself stored under a named key in the local scope dictionary" is probably closer to the truth, but is rather wordy, and sadly I can't think of a satisfactory middle ground for referring to an operation such a="foo" Commented Aug 15, 2011 at 0:02
  • The instruction a = "century" is an assignement and the related process is a binding of a name and an object
    – eyquem
    Commented Aug 15, 2011 at 0:59
25

Triple-quotes aren't comments. They're string literals that span multiple lines and include those line breaks in the resulting string. This allows you to use

somestr = """This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is\
 significant."""

instead of

somestr = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."
5

Most scripting languages use # as a comment marker so to skip automatically the shebang (#!) which specifies to the program loader the interpreter to run (like in #!/bin/bash). Alternatively, the interpreter could be instructed to automatically skip the first line, but it's way more convenient just to define # as comment marker and that's it, so it's skipped as a consequence.

0
4

Guido - the creator of Python, actually weighs in on the topic here: https://twitter.com/gvanrossum/status/112670605505077248?lang=en

In summary - for multiline comments, just use triple quotes. For academic purposes - yes it technically is a string, but it gets ignored because it is never used or assigned to a variable.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.