为什么Python没有多行注释?

时间:2022-04-10 07:18:36

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.

我搜索并阅读了Python样式指南,但是我无法找到一个技术上的答案来解释为什么没有多行/* /类型注释的正式实现。我使用三重引号没有问题,但是我有点好奇是什么导致了这个设计决定。

16 个解决方案

#1


242  

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! :-)

Python提示:可以使用多行字符串作为多行注释。除非用作docstring,否则它们不会生成代码!:-)

#2


58  

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.

糟糕,您的字符串包含结束注释分隔符。

#3


33  

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.

三引号的文本不应被视为多行注释;按照惯例,它们是docstring。它们应该描述代码的功能和使用方法,但不能用于注释代码块。

According to Guido, multiline comments in Python are just contiguous single-line comments (search for "block comments").

根据Guido的说法,Python中的多行注释只是连续的单行注释(搜索“块注释”)。

To comment blocks of code, I sometimes use the following pattern:

为了注释代码块,我有时使用以下模式:

if False:
    # A bunch of code

#4


28  

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.

这可能要追溯到核心概念,即应该有一种显而易见的方式来完成一项任务。附加的评论风格会增加不必要的复杂性,并且会降低可读性。

#5


11  

Well, the triple-quotes are used as multiline comments in docstrings. And # comments are used as inline comments and people get use to it.

在docstring中,三重引号用作多行注释。#注释被用作内联注释,人们习惯了它。

Most of script languages don't have multiline comments either. Maybe that's the cause?

大多数脚本语言也没有多行注释。也许这就是原因吗?

See PEP 0008, section Comments

参见PEP 0008节评论

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.

看看您的Python编辑器是否提供了一些用于块注释的键盘快捷方式。Emacs和Eclipse都支持它,想必大多数像样的ide都支持它。

#6


8  

From The Zen of Python:

从Python的禅宗:

There should be one-- and preferably only one --obvious way to do it.

应该有一种——最好只有一种——显而易见的方法。

#7


4  

Personally my comment style in say Java is like

就我个人而言,我在say Java中的评论风格就像

/*
 * 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

VB。NET也是一种只用一行评论的语言,我个人觉得它很烦人,因为评论看起来不那么喜欢评论,而更像是某种引用

'
' 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.

与多行注释相比,单行注释的字符使用更少,而且更不可能被regex语句中的一些不可靠字符所转义?我倾向于同意奈德。

#8


3  

# This
# is
# a 
# multi-line
# comment

Use comment block or search and replace (s/^/#/g) in your editor to achieve this.

使用注释块或搜索和替换(s / ^ # / g)在你的编辑器来实现这一目标。

#9


3  

To comment out a block of code in the Pycharm IDE:

在Pycharm IDE中注释一段代码:

  • Code | Comment with Line Comment
  • 代码|注释与行注释。
  • Windows or Linux: Ctrl + /
  • Windows或Linux: Ctrl + /。
  • Mac OS: Command + /
  • Mac OS:命令+ /

#10


2  

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.

我通过为我的文本编辑器(TextPad)下载一个宏来解决这个问题,这个宏让我高亮显示行,然后在每一行的第一行插入#。一个类似的宏删除# s。有人可能会问为什么需要多行代码,但当您试图“关闭”代码块以进行调试时,多行代码就派上了用场。

#11


0  

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,

因为#约定是一种常见的约定,对于多行注释,没有什么可以用#-sign注释做不到的。这是一个历史上的意外,就像……*/评论返回PL/I,

#12


0  

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.

假设他们被认为是不必要的。由于键入#a注释非常容易,多行注释可以包含许多单行注释。

For HTML, on the other hand, there's more of a need for multiliners. It's harder to keep typing <!--comments like this-->.

另一方面,对于HTML来说,需要更多的多衬线。要继续输入 。

#13


0  

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.

因为它们是字符串,所以它们有一些语义值(编译器不会删除它们),因此将它们用作docstring是有意义的。它们实际上成为AST的一部分,因此提取文档变得更容易。

#14


0  

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:

然后你发现你的代码中有一些你不能用调试器修复的东西,所以你开始手动调试它,用ese多行注释注释写出越来越小的代码块。这将提供函数:

void someFunction()
{ /*
    Something
   /* Comments */
   Something more*/
}

This is really irritating.

这真的很刺激。

#15


0  

Multiline comments using IDLE on:

使用空闲的多行注释:

  • Mac OS X, after code selection, comment a block of code with Ctrl+3 and uncomment using Ctrl+4.

    Mac OS X,在选择代码后,使用Ctrl+3注释一段代码,使用Ctrl+4取消注释。

  • Windows, after code selection, comment a block of code with Ctrl+Alt+3 and uncomment using Ctrl+At+4.

    在选择代码后,使用Ctrl+Alt+3注释一段代码,使用Ctrl+At+4取消注释。

#16


-1  

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

'''

#1


242  

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! :-)

Python提示:可以使用多行字符串作为多行注释。除非用作docstring,否则它们不会生成代码!:-)

#2


58  

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.

糟糕,您的字符串包含结束注释分隔符。

#3


33  

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.

三引号的文本不应被视为多行注释;按照惯例,它们是docstring。它们应该描述代码的功能和使用方法,但不能用于注释代码块。

According to Guido, multiline comments in Python are just contiguous single-line comments (search for "block comments").

根据Guido的说法,Python中的多行注释只是连续的单行注释(搜索“块注释”)。

To comment blocks of code, I sometimes use the following pattern:

为了注释代码块,我有时使用以下模式:

if False:
    # A bunch of code

#4


28  

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.

这可能要追溯到核心概念,即应该有一种显而易见的方式来完成一项任务。附加的评论风格会增加不必要的复杂性,并且会降低可读性。

#5


11  

Well, the triple-quotes are used as multiline comments in docstrings. And # comments are used as inline comments and people get use to it.

在docstring中,三重引号用作多行注释。#注释被用作内联注释,人们习惯了它。

Most of script languages don't have multiline comments either. Maybe that's the cause?

大多数脚本语言也没有多行注释。也许这就是原因吗?

See PEP 0008, section Comments

参见PEP 0008节评论

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.

看看您的Python编辑器是否提供了一些用于块注释的键盘快捷方式。Emacs和Eclipse都支持它,想必大多数像样的ide都支持它。

#6


8  

From The Zen of Python:

从Python的禅宗:

There should be one-- and preferably only one --obvious way to do it.

应该有一种——最好只有一种——显而易见的方法。

#7


4  

Personally my comment style in say Java is like

就我个人而言,我在say Java中的评论风格就像

/*
 * 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

VB。NET也是一种只用一行评论的语言,我个人觉得它很烦人,因为评论看起来不那么喜欢评论,而更像是某种引用

'
' 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.

与多行注释相比,单行注释的字符使用更少,而且更不可能被regex语句中的一些不可靠字符所转义?我倾向于同意奈德。

#8


3  

# This
# is
# a 
# multi-line
# comment

Use comment block or search and replace (s/^/#/g) in your editor to achieve this.

使用注释块或搜索和替换(s / ^ # / g)在你的编辑器来实现这一目标。

#9


3  

To comment out a block of code in the Pycharm IDE:

在Pycharm IDE中注释一段代码:

  • Code | Comment with Line Comment
  • 代码|注释与行注释。
  • Windows or Linux: Ctrl + /
  • Windows或Linux: Ctrl + /。
  • Mac OS: Command + /
  • Mac OS:命令+ /

#10


2  

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.

我通过为我的文本编辑器(TextPad)下载一个宏来解决这个问题,这个宏让我高亮显示行,然后在每一行的第一行插入#。一个类似的宏删除# s。有人可能会问为什么需要多行代码,但当您试图“关闭”代码块以进行调试时,多行代码就派上了用场。

#11


0  

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,

因为#约定是一种常见的约定,对于多行注释,没有什么可以用#-sign注释做不到的。这是一个历史上的意外,就像……*/评论返回PL/I,

#12


0  

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.

假设他们被认为是不必要的。由于键入#a注释非常容易,多行注释可以包含许多单行注释。

For HTML, on the other hand, there's more of a need for multiliners. It's harder to keep typing <!--comments like this-->.

另一方面,对于HTML来说,需要更多的多衬线。要继续输入 。

#13


0  

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.

因为它们是字符串,所以它们有一些语义值(编译器不会删除它们),因此将它们用作docstring是有意义的。它们实际上成为AST的一部分,因此提取文档变得更容易。

#14


0  

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:

然后你发现你的代码中有一些你不能用调试器修复的东西,所以你开始手动调试它,用ese多行注释注释写出越来越小的代码块。这将提供函数:

void someFunction()
{ /*
    Something
   /* Comments */
   Something more*/
}

This is really irritating.

这真的很刺激。

#15


0  

Multiline comments using IDLE on:

使用空闲的多行注释:

  • Mac OS X, after code selection, comment a block of code with Ctrl+3 and uncomment using Ctrl+4.

    Mac OS X,在选择代码后,使用Ctrl+3注释一段代码,使用Ctrl+4取消注释。

  • Windows, after code selection, comment a block of code with Ctrl+Alt+3 and uncomment using Ctrl+At+4.

    在选择代码后,使用Ctrl+Alt+3注释一段代码,使用Ctrl+At+4取消注释。

#16


-1  

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

'''