本文翻译自:How to comment out a block of code in Python [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- Why doesn't Python have multiline comments? 为什么Python没有多行注释? 17 answers 17个答案
Is there a mechanism to comment out large blocks of Python code? 有没有一种机制可以注释掉大块的Python代码?
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". 阅读完所有评论后,答案似乎是“不”。
#1楼
参考:https://stackoom.com/question/2piE/如何在Python中注释掉一段代码-重复
#2楼
Yes, there is (depending on your editor). 是的,有(取决于你的编辑)。 In PyDev (and in Aptana Studio with PyDev): 在PyDev中 (以及在带有PyDev的Aptana Studio中 ):
Ctrl + 4 - comment selected block Ctrl + 4 - 注释选定的块
Ctrl + 5 - uncomment selected block Ctrl + 5 - 取消注释所选块
#3楼
In Eclipse + PyDev , Python block commenting is similar to Eclipse Java block commenting; 在Eclipse + PyDev中 ,Python块注释类似于Eclipse Java块注释; select the lines you want to comment and use Ctrl + / to comment. 选择要评论的行并使用Ctrl + /进行评论。 To uncomment a commented block, do the same thing. 要取消注释已注释的块,请执行相同的操作。
#4楼
在使用Visual Studio的Python工具的Visual Studio中 ,可以通过Ctrl + K , Ctrl + C注释掉块,并通过Ctrl + K , Ctrl + U取消注释。
#5楼
在使用PyDev的 Eclipse中,您可以选择一个代码块并按Ctrl + # 。
#6楼
At least in VIM you can select the first column of text you want to insert using Block Visual mode ( CTRL+V
in non-windows VIMs) and then prepend a #
before each line using this sequence: 至少在VIM中,您可以使用Block Visual模式(非Windows VIM中的CTRL+V
)选择要插入的第一列文本,然后使用此序列在每行之前添加#
:
I#<esc>
In Block Visual mode I
moves to insert mode with the cursor before the block on its first line. 在Block Visual模式下, I
移动到插入模式,光标位于第一行的块之前。 The inserted text is copied before each line in the block. 插入的文本在块中的每一行之前复制。