How to comment out a block of code in Python [duplicate] How to comment out a block of code in Python [duplicate] python python

How to comment out a block of code in Python [duplicate]


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-pound-signs automatically for you. For example, in IDLE on my machine, it's Alt+3 and Alt+4.

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


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


Hide the triple quotes in a context that won't be mistaken for a docstring, eg:

'''...statements...''' and None

or:

if False: '''...statements...'''


matomo