Is there a way to create multiline comments in Python?

Is there a way to create multiline comments in Python?

You can use triple-quoted strings. When theyre not a docstring (the first thing in a class/function/module), they are ignored.


This is a multiline
comment.

(Make sure to indent the leading appropriately to avoid an IndentationError.)

Guido van Rossum (creator of Python) tweeted this as a pro tip.

However, Pythons style guide, PEP8, favors using consecutive single-line comments, like this:

# This is a multiline
# comment.

…and this is also what youll find in many projects. Text editors usually have a shortcut to do this easily.

Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode — just like #-prepended comments. In effect, it acts exactly like a comment.

On the other hand, if you say this behavior must be documented in the official documentation to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.

In any case, your text editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to a text editor that does.

Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.

Not only should the text editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and it should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.


To protect against link decay, here is the content of Guido van Rossums tweet:

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

Is there a way to create multiline comments in Python?

From the accepted answer…

You can use triple-quoted strings. When theyre not a docstring (first thing in a class/function/module), they are ignored.

This is simply not true. Unlike comments, triple-quoted strings are still parsed and must be syntactically valid, regardless of where they appear in the source code.

If you try to run this code…

def parse_token(token):
    
    This function parses a token.
    TODO: write a decent docstring :-)
    

    if token == \and:
        do_something()

    elif token == \or:
        do_something_else()

    elif token == \xor:
        
        Note that we still need to provide support for the deprecated
        token xor. Hopefully we can drop support in libfoo 2.0.
        
        do_a_different_thing()

    else:
        raise ValueError

Youll get either…

ValueError: invalid x escape

…on Python 2.x or…

SyntaxError: (unicode error) unicodeescape codec cant decode bytes in position 79-80: truncated xXX escape

…on Python 3.x.

The only way to do multi-line comments which are ignored by the parser is…

elif token == \xor:
    # Note that we still need to provide support for the deprecated
    # token xor. Hopefully we can drop support in libfoo 2.0.
    do_a_different_thing()

Leave a Reply

Your email address will not be published.