7

How come python reacts to indentations of a comment?

def foo():
    """
    Random comment
    """
    return True

works, but:

def foo():
"""
Random comment
"""
    return True

doesn't work, throwing an IndentationError.

Seems weird to me since comments shouldn't be nothing more then comments. And by the way, this works:

def foo():
# Another random comment
    return True
4
  • You probably mean that the second code snippet does throw an IndentionError. Commented Aug 8, 2012 at 7:54
  • 2
    @Tichodroma actually the OP means the second code snippet doesn't work, throwing an IndentationError Commented Aug 8, 2012 at 8:02
  • 1
    Ah, English here at Stack Overflow! Commented Aug 8, 2012 at 8:03
  • Because it isn't a comment. Commented Dec 7, 2023 at 10:46

2 Answers 2

14

The tripple-quoted string is not a comment; it is the docstring of the method. You can access it with foo.__doc__ later, for example, or have it formatted for you with help(foo). Tripple-quoting (""" or ''') is a python-specific method of specifying a string literal where newlines do not need to be escaped.

As such, it is part of the body of the function, and thus needs to be indented to match. In fact, any string appearing as the first statement of a function is treated as a docstring, single quoting would work too. The same trick works for classes and modules too.

A lot of tools can make use of this documentation string; you can embed doctest tests in this information, for example. See PEP 257 for conventions on formatting this string.

Comments on the other hand, are always denoted by a # (where not part of a string literal) and are ignored up to the end of a line. If all the line contains is a comment, the whole line is thus ignored, as would a line with only whitespace. See the documentation on comments.

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

5 Comments

You should add that comments in Python only start with # and nothing else.
I believe you are overemphasizing the docstring-ness. The indentation issue has nothing to do with docstrings per se. It just has to do with strings.
with pep 257 you have to read comments section from pep8 python.org/dev/peps/pep-0008/#comments
@BrenBarn: Education about docstrings is just as important here; recognition that the first string literal is not a comment is the main point, as the OP clearly knows about the # syntax.
@MartijnPieters: Your answer only applies if the string literal is the first thing in the block, but from the OP's phrasing it appears likely he thinks all triple-quoted strings are comments, which is why I think it's broader than just docstrings.
3

The triple-quoted string is not a comment, it is a string literal. It's not assigned or used in any way by your code, but it's still a regular string and has to fit the syntax of Python. (In this case it happens to be the docstring, but that has nothing to do with whether indentation matters for it or not.)

# is the way to get comments.

4 Comments

@Martijn Peters: I assume you mean it's the docstring? It's not assigned or used in the code. It happens to be the docstring, but that has nothing to do with why indentation is or isn't required. I edited to clarify that it is the docstring, though.
Not assigned or used in his examples here, but it is assigned! Your statement is at best confusing.
@MartijnPieters What is it assigned to? And quoting BrenBarn in full, what is it "assigned to by your [the OP's] code"?
@shambulator: the interpreter assigns it to the .__doc__ attribute of the function, class or module. No explicit assigment operator is required, although foo.__doc__ = 'your new docstring' works just fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.