Home python Multi-line comments in Python

Multi-line comments in Python

Author

Date

Category

In Python, single line comments use the # character. But if you need to comment out a large block of code, you have to add # at the beginning of each line. This is very inconvenient when debugging.

Is there some way to use multi-line comments (analogous to / * ... * / from C) in Python?

UPD:

I know that various IDEs allow you to do this kind of thing automatically, but I would like a more elegant solution that does not depend on the code editing tool and various utilities.


Answer 1, authority 100%

As far as I know, there is no separate syntax for multiline comments in Python. At the same time, you can use string literals enclosed in triple apostrophes, for example:

'' '
It is a string literal.
But here it is used as a multi-line comment
'' '

String literals enclosed in triple quotes can contain:

  • quotes (")
  • apostrophes (')
  • docstring comments ("" "func desc" "" )
  • line feeds

At the same time, it is worth remembering that such a string literal must not contain '' 'characters inside. This requirement is similar to disallowing the * / character sequence within a multiline C comment.

By the way, this same hack suggests using the creator of the Python language in one of his tweets .

At the same time, as @jfs correctly pointed out, style guide code (pep-8) recommends using # for block comments.


Answer 2, authority 22%

Code Style Guide (pep-8) recommends using # for block comments.

But if you need to comment out a large block of code, you have to add # at the beginning of each line. This is very inconvenient when debugging.

One of the clear signs of poor programming is the presence of commented-out code fragments. Use a version control system and / or your IDE to temporarily remove / comment out the code while debugging. Customize your environment so that you can do it without hesitation by pressing a couple of keys.

I know that various IDEs allow you to do this kind of thing automatically, but I would like a more elegant solution that does not depend on the code editing tool and various utilities.

Commented out code should not be added to version control, so for temporary changes that won’t survive a single editing session, a single key chord (like M-; in Emacs) is usually sufficient to comment out / uncomment a piece of code .


"" "multiline string literal" "" is not a multiline comment in Python. It’s just a string constant that allows literal newlines without escaping (such as \ n ). Often used to describe modules, classes, functions / methods right in the code:

& gt; & gt; & gt; def f ():
... "" "abc" ""
...
& gt; & gt; & gt; f .__ doc__
'abc'

Attempting to use "" "" "" as a multi-line comment will break at the first docstring, even if there were no other more suitable solutions for the task.


Answer 3, authority 11%

Almost all editors can select text in columns.

Most of them do this with Alt + Shift + Arrow, while Idea editors use Alt + Shift + Insert to switch between normal and columnar modes.

When I comment out code, I always use single-line comments, highlighting the desired group of lines. This is convenient, because, firstly, the fact of being commented out does not depend on the presence of comments in the commented code, and secondly, a single commented-out section of code is immediately clearly visible (when commenting with the comment button, the IDE ignores empty lines).

PS: If the code is commented for long-term purposes, then I put 4 slashes instead of two.


Answer 4, authority 7%

In PyCharm , you can select the block that you want to comment out and press Ctrl + /
PyCharm will put ‘#’ at the beginning of each line
If you need to uncomment, you need to press Ctrl + / again and ‘#’ will be removed!
Very convenient!


Answer 5, authority 4%

Multi-line comments in python with only "" "(three quotes)

Previous articlejquery dropdown
Next articleRussian in the console

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions