问题:为什么Python没有多行注释?
好的,我知道三引号字符串可以用作多行注释。例如,
"""Hello, I am a
multiline comment"""
和
'''Hello, I am a
multiline comment'''
但是从技术上讲,这些是字符串,对吗?
我已经在Google上搜索并阅读了Python样式指南,但是无法找到关于为什么没有正式实现多行,/ * * /注释类型的技术答案。我使用三重引号没有问题,但是我对导致这个设计决定的原因有点好奇。
OK, I’m aware that triple-quotes strings can serve as multiline comments. For example,
"""Hello, I am a
multiline comment"""
and
'''Hello, I am a
multiline comment'''
But technically speaking these are strings, correct?
I’ve googled and read the Python style guide, but I was unable to find a technical answer to why there is no formal implementation of multiline, /* */ type of comments. I have no problem using triple quotes, but I am a little curious as to what led to this design decision.
回答 0
我怀疑您会得到比“ Guido不需要多行注释”更好的答案。
Guido在推特上发布了以下内容:
Python提示:您可以将多行字符串用作多行注释。除非用作文档字符串,否则它们不会生成任何代码!:-)
I doubt you’ll get a better answer than, “Guido didn’t feel the need for multi-line comments”.
Guido has tweeted about this:
Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)
回答 1
多行注释很容易被破坏。如果您在一个简单的计算器程序中拥有以下内容,该怎么办?
operation = ''
print("Pick an operation: +-*/")
# Get user input here
尝试用多行注释对此进行注释:
/*
operation = ''
print("Pick an operation: +-*/")
# Get user input here
*/
糟糕,您的字符串包含结尾注释定界符。
Multi-line comments are easily breakable. What if you have the following in a simple calculator program?
operation = ''
print("Pick an operation: +-*/")
# Get user input here
Try to comment that with a multi-line comment:
/*
operation = ''
print("Pick an operation: +-*/")
# Get user input here
*/
Oops, your string contains the end comment delimiter.
回答 2
用三引号括起来的文本不应视为多行注释;按照惯例,它们是docstrings。他们应该描述您的代码做什么以及如何使用它,而不是描述诸如注释代码块之类的内容。
根据Guido的说法,Python中的多行注释只是连续的单行注释(搜索“块注释”)。
为了注释代码块,我有时使用以下模式:
if False:
# A bunch of code
Triple-quoted text should NOT be considered multi-line comments; by convention, they are docstrings. They should describe what your code does and how to use it, but not for things like commenting out blocks of code.
According to Guido, multiline comments in Python are just contiguous single-line comments (search for “block comments”).
To comment blocks of code, I sometimes use the following pattern:
if False:
# A bunch of code
回答 3
这可能回到核心概念,即应该有一种显而易见的方法来完成一项任务。其他注释样式会增加不必要的复杂性,并可能降低可读性。
This likely goes back to the core concept that there should be one obvious way to do a task. Additional comment styles add unnecessary complications and could decrease readability.
回答 4
好吧,三引号用作文档字符串中的多行注释。#注释用作内联注释,人们逐渐习惯了。
大多数脚本语言也没有多行注释。也许是原因所在?
参见PEP 0008,注释部分
并查看您的Python编辑器是否提供了一些用于块注释的键盘快捷键。Emacs以及Eclipse都支持它,大概大多数体面的IDE都支持。
Well, the triple-quotes are used as multiline comments in docstrings. And # comments are used as inline comments and people get use to it.
Most of script languages don’t have multiline comments either. Maybe that’s the cause?
See PEP 0008, section Comments
And see if your Python editor offers some keyboard shortcut for block commenting. Emacs supports it, as well as Eclipse, presumably most of decent IDEs does.
回答 5
From The Zen of Python:
There should be one– and preferably only one –obvious way to do it.
回答 6
就我个人而言,Java的评论风格就像
/*
* My multi-line comment in Java
*/
因此,如果您的样式是前面示例的典型样式,那么仅具有单行注释并不是一件坏事,因为相比之下,您将拥有
#
# My multi-line comment in Python
#
VB.NET也是一种仅具有单行注释的语言,我个人认为它很烦人,因为注释最终看起来不像注释,而更像某种引用
'
' This is a VB.NET example
'
仅单行注释最终会比多行注释具有更少的字符使用率,并且在正则表达式语句中不太可能被某些狡猾的字符转义?不过,我倾向于同意内德。
Personally my comment style in say Java is like
/*
* My multi-line comment in Java
*/
So having single-line only comments isn’t such a bad thing if your style is typical to the preceding example because in comparison you’d have
#
# My multi-line comment in Python
#
VB.NET is also a language with single-line only commenting, and personally I find it annoying as comments end up looking less likes comments and more like some kind of quote
'
' This is a VB.NET example
'
Single-line-only comments end up having less character-usage than multi-line comments, and are less likely to be escaped by some dodgy characters in a regex statement perhaps? I’d tend to agree with Ned though.
回答 7
在Pycharm IDE中注释掉一段代码:
- 代码 带线注释
- Windows或Linux:Ctrl+/
- Mac OS:Command+/
To comment out a block of code in the Pycharm IDE:
- Code | Comment with Line Comment
- Windows or Linux: Ctrl + /
- Mac OS: Command + /
回答 8
# This
# is
# a
# multi-line
# comment
使用注释块或在编辑器中搜索并替换(s / ^ /#/ g)即可实现此目的。
# This
# is
# a
# multi-line
# comment
Use comment block or search and replace (s/^/#/g) in your editor to achieve this.
回答 9
我通过为文本编辑器(TextPad)下载宏解决了这一问题,该宏使我可以突出显示行,然后在每行的第一行插入#。类似的宏将删除#。有人可能会问为什么需要多行,但是当您尝试“关闭”代码块以进行调试时,它会派上用场。
I solved this by downloading a macro for my text editor (TextPad) that lets me highlight lines and it then inserts # at the first of each line. A similar macro removes the #’s. Some may ask why multiline is necessary but it comes in handy when you’re trying to “turn off” a block of code for debugging purposes.
回答 10
对于在Python中寻找多行注释的其他人-使用三引号格式可能会产生一些问题,因为我刚刚学到了很难的方法。考虑一下:
this_dict = {
'name': 'Bob',
"""
This is a multiline comment in the middle of a dictionary
"""
'species': 'Cat'
}
多行注释将被塞入下一个字符串中,从而弄乱了
'species'
密钥。最好仅#
用于评论。
For anyone else looking for multi-line comments in Python – using the triple quote format can have some problematic consequences, as I’ve just learned the hard way. Consider this:
this_dict = {
'name': 'Bob',
"""
This is a multiline comment in the middle of a dictionary
"""
'species': 'Cat'
}
The multi-line comment will be tucked into the next string, messing up the
'species'
key. Better to just use #
for comments.
回答 11
因为#约定是常见的约定,所以对于多行注释,您实际上无能为力,而对#符号注释则无能为力。这是历史性的意外,就像/* ... */
回溯到PL / I 的评论之初一样,
Because the # convention is a common one, and there really isn’t anything you can do with a multiline comment that you can’t with a #-sign comment. It’s a historical accident, like the ancestry of /* ... */
comments going back to PL/I,
回答 12
假设只是认为它们是不必要的。由于键入非常容易,因此#a comment
多行注释只能包含许多单行注释。
另一方面,对于HTML,更需要多行。很难继续打字<!--comments like this-->
。
Assume that they were just considered unnecessary. Since it’s so easy to just type #a comment
, multiline comments can just consist of many single line comments.
For HTML, on the other hand, there’s more of a need for multiliners. It’s harder to keep typing <!--comments like this-->
.
回答 13
这只是一个猜测..但是
因为它们是字符串,所以它们具有一些语义值(编译器不会摆脱它们),因此将它们用作文档字符串是有意义的。它们实际上成为AST的一部分,因此提取文档变得更加容易。
This is just a guess .. but
Because they are strings, they have some semantic value (the compiler doesn’t get rid of them), therefore it makes sense for them to be used as docstrings. They actually become part of the AST, so extracting documentation becomes easier.
回答 14
此外,多行注释是一个bit子。抱歉地说,但是不管使用哪种语言,我都不会将它们用于调试目的。假设您有这样的代码:
void someFunction()
{
Something
/*Some comments*/
Something else
}
然后,您会发现代码中有些内容无法使用调试器进行修复,因此您可以通过使用多行注释注释掉越来越小的代码块来开始手动调试它。然后将提供以下功能:
void someFunction()
{ /*
Something
/* Comments */
Something more*/
}
这真令人讨厌。
Besides, multiline comments are a bitch. Sorry to say, but regardless of the language, I don’t use them for anything else than debugging purposes. Say you have code like this:
void someFunction()
{
Something
/*Some comments*/
Something else
}
Then you find out that there is something in your code you can’t fix with the debugger, so you start manually debugging it by commenting out smaller and smaller chuncks of code with theese multiline comments. This would then give the function:
void someFunction()
{ /*
Something
/* Comments */
Something more*/
}
This is really irritating.
回答 15
Multiline comments using IDLE on:
Mac OS X, after code selection, comment a block of code with Ctrl+3 and uncomment using Ctrl+4.
Windows, after code selection,
comment a block of code with Ctrl+Alt+3 and uncomment using Ctrl+At+4.
回答 16
我记得读过一篇关于将多行注释放入三引号中的变量的家伙的文章:
x = '''
This is my
super-long mega-comment.
Wow there are a lot of lines
going on here!
'''
这确实占用了一些内存,但是它为您提供了多行注释功能,并且大多数编辑器都会为您突出显示语法:)
通过简单地将其包装起来,注释掉代码也很容易
x = '''
和
'''
I remember reading about one guy who would put his multi-line comments into a triple-quoted variable:
x = '''
This is my
super-long mega-comment.
Wow there are a lot of lines
going on here!
'''
This does take up a bit of memory, but it gives you multi-line comment functionality, and plus most editors will highlight the syntax for you :)
It’s also easy to comment out code by simply wrapping it with
x = '''
and
'''