如何注释掉Python中的代码块
有没有一种机制可以注释掉大块Python代码?
目前,我能看到的注释代码的唯一方法是用 #
开始每一行,或者将代码括在三引号中:"""
。
这些的问题在于,在每行之前插入 #
很麻烦,并且 """
会使我想要用作注释的字符串显示在生成的文档中。
看完所有评论后,答案似乎是“不”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
Python没有这样的机制。 在每行前面添加一个
#
以阻止注释。 有关详细信息,请参阅 PEP 8。 大多数 Python IDE 支持一种自动为您执行带有哈希符号的块注释的机制。 例如,在我的机器上的 IDLE 中,它是 Alt+3 和 Alt+4。不要使用三引号; 正如您所发现的,这是用于文档字符串而不是阻止注释,尽管它具有类似的效果。 如果您只是暂时注释掉一些内容,那么作为临时措施就可以了。
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, 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.
将三引号隐藏在不会被误认为文档字符串的上下文中,例如:
或:
Hide the triple quotes in a context that won't be mistaken for a docstring, eg:
or:
我知道解决这个问题的唯一方法是一个好的编辑。 对不起。
The only cure I know for this is a good editor. Sorry.
无需三引号即可执行此操作的唯一方法是添加:,
然后缩进所有代码。 请注意,代码仍然需要具有正确的语法。
许多 Python IDE 可以在每个选定的行上添加
#
,并在取消注释时将其删除。 同样,如果您使用 vi 或 Emacs 您可以创建一个宏来为您的代码块执行此操作。The only way you can do this without triple quotes is to add an:
And then indent all your code. Note that the code will still need to have proper syntax.
Many Python IDEs can add
#
for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.在 Mac 上的 JetBrains PyCharm 中,使用 Command + / 注释/取消注释选定的代码块。 在 Windows 上,使用 CTRL + /。
In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.
Mx 注释区域,在 Emacs' Python 模式下。
M-x comment-region, in Emacs' Python mode.
至少在 VIM 中,您可以使用 Block Visual 模式(非 Windows VIM 中的
CTRL+V
)选择要插入的第一列文本,然后在前面添加#
每行都使用此序列:在块可视模式下,
I
移动到插入模式,光标位于第一行的块之前。 插入的文本将复制到块中的每行之前。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:In Block Visual mode
I
moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.在 vi 中:
然后执行
In vi:
Then do
您可以将
comm
替换为您选择的变量,该变量可能更短、易于触摸输入,并且您知道它不会(也不会)出现在您的程序中。 示例:xxx
、oo
、null
、nil
。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
.在使用 Python Tools for Visual Studio 的 Visual Studio 中,可以通过 Ctrl+K, Ctrl+C 并通过 Ctrl+K 取消注释,< kbd>Ctrl+U。
In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.
我在 Windows 计算机上使用 Notepad++,选择您的代码,输入
CTRL-K. 要取消注释,请选择代码并按 Ctrl + Shift + K。
顺便说一句,Notepad++ 作为 Python 编辑器运行得很好。 具有自动完成、代码折叠、语法突出显示等功能。 而且它就像言论和啤酒一样免费!
I use Notepad++ on a Windows machine, select your code, type
CTRL-K
. To uncomment you select code and press Ctrl + Shift + K.Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it's free as in speech and as in beer!
在 Eclipse + PyDev,Python 块注释与 Eclipse Java 块注释类似; 选择要注释的行并使用 Ctrl + / 进行注释。 要取消注释块,请执行相同的操作。
In Eclipse + PyDev, Python block commenting is similar to Eclipse Java block commenting; select the lines you want to comment and use Ctrl + / to comment. To uncomment a commented block, do the same thing.
是的,有(取决于您的编辑)。 在 PyDev 中(以及在 Aptana Studio 与 PyDev):
Ctrl + 4 - 注释所选块
Ctrl + 5 - 取消注释所选块
Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):
Ctrl + 4 - comment selected block
Ctrl + 5 - uncomment selected block
注释掉 Python 代码(理解为被解释器忽略的代码)的唯一机制是 # 。
正如您所说,您还可以使用 字符串文字,它们不是被解释器忽略,但可能与程序执行完全无关。
The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.
As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.
在Eclipse中使用PyDev,您可以选择一个代码块并按 Ctrl + #。
In Eclipse using PyDev, you can select a code block and press Ctrl + #.
三引号对我来说没问题。
您可以使用 ''' foo ''' 作为文档字符串,使用 """ bar """ 作为注释,反之亦然,以使代码更具可读性。
Triple quotes are OK to me.
You can use ''' foo ''' for docstrings and """ bar """ for comments or vice-versa to make the code more readable.
另一个基于编辑器的解决方案:Emacs 中的文本“矩形”。
突出显示要注释掉的代码,然后 Cxrt #
要取消注释代码:突出显示,然后 Cxrk
我每天都用这个。 (当然,分配给热键。)
这种强大的正则表达式搜索/替换是我容忍 Emacs 的其他“怪癖”的原因。
Another editor-based solution: text "rectangles" in Emacs.
Highlight the code you want to comment out, then C-x-r-t #
To un-comment the code: highlight, then C-x-r-k
I use this all-day, every day. (Assigned to hot-keys, of course.)
This and powerful regex search/replace is the reason I tolerate Emacs's other "eccentricities".
在 Eric4 上有一个简单的方法:选择一个块,输入 Ctrl+M 来注释整个块或 Ctrl+alt< /kbd>+M 取消注释。
On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.
使用 SciTe 等不错的编辑器,选择您的代码,按 Ctrl + Q 完成。
如果您没有支持块注释的编辑器,您可以在代码块的开头和结尾使用三引号字符串来“有效”地注释掉它。 但这不是最佳实践。
Use a nice editor like SciTe, select your code, press Ctrl + Q and done.
If you don't have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to 'effectively' comment it out. It is not the best practice though.