Comment out a Python code block

This question already has an answer here:

  • Why doesn't Python have multiline comments? 16 answers

  • 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...
    '''
    
    链接地址: http://www.djcxy.com/p/42720.html

    上一篇: 如何使用逗号作为数千个分隔符来打印数字?

    下一篇: 注释掉一个Python代码块