How to comment out a block of code in Python [dupl

2019-01-12 14:01发布

This question already has an answer here:

Is there a mechanism to comment out large blocks of Python code?

Right now, the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in triple quotes: """.

The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation.

After reading all comments, the answer seems to be "No".

19条回答
放我归山
2楼-- · 2019-01-12 14:31

In vi:

  • Go to top of block and mark it with letter a.
  • Go to bottom of block and mark it with letter b

Then do

:'a,'b s!^!#!
查看更多
相关推荐>>
3楼-- · 2019-01-12 14:32

On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.

查看更多
Juvenile、少年°
4楼-- · 2019-01-12 14:33

In Eclipse using PyDev, you can select a code block and press Ctrl + #.

查看更多
做个烂人
5楼-- · 2019-01-12 14:34

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

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

or:

if False: '''
...statements...
'''
查看更多
祖国的老花朵
6楼-- · 2019-01-12 14:34

M-x comment-region, in Emacs' Python mode.

查看更多
一夜七次
7楼-- · 2019-01-12 14:34
comm='''
Junk, or working code 
that I need to comment.
'''

You can replace comm by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx, oo, null, nil.

查看更多
登录 后发表回答