Explain Codes LogoExplain Codes Logo

How do I create multiline comments in Python?

python
commenting-strategy
docstrings
code-readability
Alex KataevbyAlex Kataev·Feb 12, 2025
TLDR

In Python, triple-quoted strings """...""" serve as a form of multiline comment:

""" This 'comment' block is ignored by the Python interpreter. """

However, these are not genuine comments but rather string literals. For a true comment, Python employs the # symbol. Any text after a # within a line is not executed by Python.

Main takeaway: Triple quotes """ for multiline 'comments', # for single-line genuine comments. Triple quotes are not discarded by Python and utilize memory; use prudently.

The Python comments strategy

Python's distinct approach to comments

Python has opted for a simpler approach to commenting. Unlike other languages such as Java or C that provide explicit syntax for multiline comments (e.g., /*...*/), Python has willingly omitted native multiline comment support. This approach aligns with Python's lean toward readability and simplicity.

What triples quotes in Python really indicate

Triple-quoted strings '''...''' or """...""" serve a dual purpose in Python. They are primarily used to define multiline strings and are also deployed to create multiline 'comments'. Remember, though, that these 'comments' are actual string literals which are not disregarded by Python as real # comments are. Erroneous or unmeasured use of these can lead to bugs and memory consumption.

Effectively disabling blocks of code

To 'comment-out' lines of code, a common practice is to use consecutive single-line comments, each prefaced with #. Conventionally aligned with PEP 8 guidelines, this practice brings into play the value of text editor shortcuts for comments.

Documentation and comments via Docstrings

Docstrings – a form of structured comment denoted by triple-quotes and placed at the start of Python classes, modules, functions, and methods – are used to document your code. Unlike 'commented' triple-quoted strings, Docstrings form part of Python’s runtime environment as they can be programmatically accessed via their .__doc__ attribute.

The art of commenting

Mantra: Indentation, indentation, indentation

Appropriate indentation is crucial to avoid syntax errors in Python, especially relevant when constructing block comments. Triple-quoted strings used as comments must not contain nested triple quotes, else they'd terminate the string literal prematurely.

Code readability: beyond mere functionality

Strive to achieve optimal code readability. Code cluttered with comments, especially block 'comments', can decrease readability. Conciseness is key – your comments should elucidate the why, not rehash the what.

Harness the power of your text editor

Leverage text editors such as Notepad++, gVim, or Emacs which offer comment-toggling shortcuts and easy indentation adjustments, to make comment management more effortless.

Commenting tips for Python coders

Don't fall into the trap of over-commenting

While triple-quoted strings appear to be a reliable default, excessive usage can lead to performance issues and ambiguity running amok. Keep these primarily for docstrings and true multiline strings.

Comments that clarify

Comments are your opportunity to shed light on the logic behind your code. They do best when they clarify complex calculations, justify design choices, or explain a bug workaround.

Modular documentation with docstrings

Make use of docstrings strategically placed in your modules, functions, and classes to clearly explain their purpose and usage. Unlike standard comments, docstrings can be accessed programmatically and utilized by automated documentation generators like Sphinx.