Python中的多行注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21253148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple line comment in Python
提问by Gaurav Rai
Is there a way to give multiple line comments in Python?
有没有办法在 Python 中给出多行注释?
Like it is in case of C/C++ : /*comment*/. Or does it have to be marked "#" in front of every line?
就像在 C/C++ 的情况下一样:/*comment*/。还是必须在每一行前面标记“#”?
采纳答案by user3218338
Try this
尝试这个
'''
This is a multiline
comment. I can type here whatever I want.
'''
Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode -- just like #-prepended comments. In effect, it acts exactly like a comment.
Python 确实具有多行字符串/注释语法,除非用作文档字符串,否则多行字符串不会生成字节码——就像 #-prepended 注释一样。实际上,它的作用与注释完全一样。
On the other hand, if you say this behavior must be documented in the official docs to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.
另一方面,如果你说这种行为必须在官方文档中记录才能成为真正的注释语法,那么是的,你说它不能保证作为语言规范的一部分是正确的。
In any case your editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to an editor that does.
在任何情况下,您的编辑器也应该能够轻松地注释掉选定的区域(通过在每行前面单独放置一个 #)。如果没有,请切换到有的编辑器。
Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.
在没有某些文本编辑功能的 Python 中编程可能是一种痛苦的经历。找到合适的编辑器(并知道如何使用它)可以对 Python 编程体验的感知方式产生很大的影响。
Not only should the editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.
编辑器不仅应该能够注释掉选定的区域,还应该能够轻松地左右移动代码块,并且在您按下 Enter 键时应该自动将光标置于当前缩进级别。代码折叠也很有用。
回答by Ashoka Lella
#Single line
'''
multi-line
comment
'''
"""
also,
multi-line comment
"""

