docstring – How to comment out a block of code in Python

docstring – How to comment out a block of code in Python

Python does not have such a mechanism. Prepend a # to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, its Alt+3 and Alt+4.

Dont use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If youre just commenting things out temporarily, this is fine as a temporary measure.

The only cure I know for this is a good editor. Sorry.

docstring – How to comment out a block of code in Python

Hide the triple quotes in a context that wont be mistaken for a docstring, eg:


...statements...
 and None

or:

if False: 
...statements...

Leave a Reply

Your email address will not be published.