为什么python使用非常规的三引号来评论?

时间:2021-09-19 06:20:04

Why didn't python just use the traditional style of comments like C/C++/Java uses:

为什么python不使用C / C ++ / Java使用的传统风格的注释:

/**
 * Comment lines 
 * More comment lines
 */

// line comments
// line comments
//

Is there a specific reason for this or is it just arbitrary?

是否有特定原因或者它是否随意?

4 个解决方案

#1


77  

Python doesn't use triple quotation marks for comments. Comments use the hash (a.k.a. pound) character:

Python不会使用三引号进行注释。注释使用哈希(a.k.a.磅)字符:

# this is a comment

The triple quote thing is a doc string, and, unlike a comment, is actually available as a real string to the program:

三重引用的东西是一个文档字符串,与注释不同,它实际上可以作为程序的真实字符串:

>>> def bla():
...     """Print the answer"""
...     print 42
...
>>> bla.__doc__
'Print the answer'
>>> help(bla)
Help on function bla in module __main__:

bla()
    Print the answer

It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention (and has the advantage of being multiline).

只要它是一个字符串,就不是严格要求使用三重引号。使用“”只是一种约定(并且具有多线的优点)。

#2


41  

A number of the answers got many of the points, but don't give the complete view of how things work. To summarize...

许多答案得到了许多要点,但并未完全了解事情的运作方式。总结一下......

# comment is how Python does actual comments (similar to bash, and some other languages). Python only has "to the end of the line" comments, it has no explicit multi-line comment wrapper (as opposed to javascript's /* .. */). Most Python IDEs let you select-and-comment a block at a time, this is how many people handle that situation.

#comment是Python如何进行实际评论(类似于bash和其他一些语言)。 Python只有“到行尾”的注释,它没有明确的多行注释包装(而不是javascript的/ * .. * /)。大多数Python IDE允许您一次选择并注释一个块,这是有多少人处理这种情况。

Then there are normal single-line python strings: They can use ' or " quotation marks (eg 'foo' "bar"). The main limitation with these is that they don't wrap across multiple lines. That's what multiline-strings are for: These are strings surrounded by triple single or double quotes (''' or """) and are terminated only when a matching unescaped terminator is found. They can go on for as many lines as needed, and include all intervening whitespace.

然后有正常的单行python字符串:它们可以使用'或'引号(例如'foo'“bar”)。这些的主要限制是它们不会包裹多行。这就是多行字符串是什么for:这些是由三个单引号或双引号('''或“”“)包围的字符串,仅在找到匹配的非转义终结符时终止。它们可以根据需要继续使用多行,并包括所有插入的空格。

Either of these two string types define a completely normal string object. They can be assigned a variable name, have operators applied to them, etc. Once parsed, there are no differences between any of the formats. However, there are two special cases based on where the string is and how it's used...

这两种字符串类型中的任何一种都定义了完全正常的字符串对象可以为它们分配变量名称,将运算符应用于它们等。一旦解析,任何格式之间都没有差异。但是,根据字符串的位置以及字符串的使用方式,有两种特殊情况......

First, if a string just written down, with no additional operations applied, and not assigned to a variable, what happens to it? When the code executes, the bare string is basically discarded. So people have found it convenient to comment out large bits of python code using multi-line strings (providing you escape any internal multi-line strings). This isn't that common, or semantically correct, but it is allowed.

首先,如果一个字符串刚刚写下来,没有应用额外的操作,并且没有分配给变量,它会发生什么?代码执行时,基本上丢弃了裸字符串。因此人们发现使用多行字符串注释掉大量的python代码很方便(允许你转义任何内部多行字符串)。这不是常见的,或在语义上是正确的,但它是允许的。

The second use is that any such bare strings which follow immediately after a def Foo(), class Foo(), or the start of a module, are treated as string containing documentation for that object, and stored in the __doc__ attribute of the object. This is the most common case where strings can seem like they are a "comment". The difference is that they are performing an active role as part of the parsed code, being stored in __doc__... and unlike a comment, they can be read at runtime.

第二个用法是在def Foo(),类Foo()或模块的开头之后立即执行的任何此类裸字符串被视为包含该对象的文档的字符串,并存储在该对象的__doc__属性中。这是最常见的情况,其中字符串看起来像是“评论”。不同之处在于它们作为已解析代码的一部分执行活动角色,存储在__doc __...中,与注释不同,它们可以在运行时读取。

#3


16  

Triple-quotes aren't comments. They're string literals that span multiple lines and include those line breaks in the resulting string. This allows you to use

三引号不是评论。它们是跨越多行的字符串文字,并在结果字符串中包含这些换行符。这允许你使用

somestr = """This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is\
 significant."""

instead of

somestr = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

#4


5  

Most scripting languages use # as a comment marker so to skip automatically the shebang (#!) which specifies to the program loader the interpreter to run (like in #!/bin/bash). Alternatively, the interpreter could be instructed to automatically skip the first line, but it's way more convenient just to define # as comment marker and that's it, so it's skipped as a consequence.

大多数脚本语言都使用#作为注释标记,因此要自动跳过shebang(#!),它指定要运行解释器的程序加载器(如#!/ bin / bash)。或者,可以指示解释器自动跳过第一行,但是将#定义为注释标记更方便,就是这样,因此它被跳过了。

#1


77  

Python doesn't use triple quotation marks for comments. Comments use the hash (a.k.a. pound) character:

Python不会使用三引号进行注释。注释使用哈希(a.k.a.磅)字符:

# this is a comment

The triple quote thing is a doc string, and, unlike a comment, is actually available as a real string to the program:

三重引用的东西是一个文档字符串,与注释不同,它实际上可以作为程序的真实字符串:

>>> def bla():
...     """Print the answer"""
...     print 42
...
>>> bla.__doc__
'Print the answer'
>>> help(bla)
Help on function bla in module __main__:

bla()
    Print the answer

It's not strictly required to use triple quotes, as long as it's a string. Using """ is just a convention (and has the advantage of being multiline).

只要它是一个字符串,就不是严格要求使用三重引号。使用“”只是一种约定(并且具有多线的优点)。

#2


41  

A number of the answers got many of the points, but don't give the complete view of how things work. To summarize...

许多答案得到了许多要点,但并未完全了解事情的运作方式。总结一下......

# comment is how Python does actual comments (similar to bash, and some other languages). Python only has "to the end of the line" comments, it has no explicit multi-line comment wrapper (as opposed to javascript's /* .. */). Most Python IDEs let you select-and-comment a block at a time, this is how many people handle that situation.

#comment是Python如何进行实际评论(类似于bash和其他一些语言)。 Python只有“到行尾”的注释,它没有明确的多行注释包装(而不是javascript的/ * .. * /)。大多数Python IDE允许您一次选择并注释一个块,这是有多少人处理这种情况。

Then there are normal single-line python strings: They can use ' or " quotation marks (eg 'foo' "bar"). The main limitation with these is that they don't wrap across multiple lines. That's what multiline-strings are for: These are strings surrounded by triple single or double quotes (''' or """) and are terminated only when a matching unescaped terminator is found. They can go on for as many lines as needed, and include all intervening whitespace.

然后有正常的单行python字符串:它们可以使用'或'引号(例如'foo'“bar”)。这些的主要限制是它们不会包裹多行。这就是多行字符串是什么for:这些是由三个单引号或双引号('''或“”“)包围的字符串,仅在找到匹配的非转义终结符时终止。它们可以根据需要继续使用多行,并包括所有插入的空格。

Either of these two string types define a completely normal string object. They can be assigned a variable name, have operators applied to them, etc. Once parsed, there are no differences between any of the formats. However, there are two special cases based on where the string is and how it's used...

这两种字符串类型中的任何一种都定义了完全正常的字符串对象可以为它们分配变量名称,将运算符应用于它们等。一旦解析,任何格式之间都没有差异。但是,根据字符串的位置以及字符串的使用方式,有两种特殊情况......

First, if a string just written down, with no additional operations applied, and not assigned to a variable, what happens to it? When the code executes, the bare string is basically discarded. So people have found it convenient to comment out large bits of python code using multi-line strings (providing you escape any internal multi-line strings). This isn't that common, or semantically correct, but it is allowed.

首先,如果一个字符串刚刚写下来,没有应用额外的操作,并且没有分配给变量,它会发生什么?代码执行时,基本上丢弃了裸字符串。因此人们发现使用多行字符串注释掉大量的python代码很方便(允许你转义任何内部多行字符串)。这不是常见的,或在语义上是正确的,但它是允许的。

The second use is that any such bare strings which follow immediately after a def Foo(), class Foo(), or the start of a module, are treated as string containing documentation for that object, and stored in the __doc__ attribute of the object. This is the most common case where strings can seem like they are a "comment". The difference is that they are performing an active role as part of the parsed code, being stored in __doc__... and unlike a comment, they can be read at runtime.

第二个用法是在def Foo(),类Foo()或模块的开头之后立即执行的任何此类裸字符串被视为包含该对象的文档的字符串,并存储在该对象的__doc__属性中。这是最常见的情况,其中字符串看起来像是“评论”。不同之处在于它们作为已解析代码的一部分执行活动角色,存储在__doc __...中,与注释不同,它们可以在运行时读取。

#3


16  

Triple-quotes aren't comments. They're string literals that span multiple lines and include those line breaks in the resulting string. This allows you to use

三引号不是评论。它们是跨越多行的字符串文字,并在结果字符串中包含这些换行符。这允许你使用

somestr = """This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is\
 significant."""

instead of

somestr = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

#4


5  

Most scripting languages use # as a comment marker so to skip automatically the shebang (#!) which specifies to the program loader the interpreter to run (like in #!/bin/bash). Alternatively, the interpreter could be instructed to automatically skip the first line, but it's way more convenient just to define # as comment marker and that's it, so it's skipped as a consequence.

大多数脚本语言都使用#作为注释标记,因此要自动跳过shebang(#!),它指定要运行解释器的程序加载器(如#!/ bin / bash)。或者,可以指示解释器自动跳过第一行,但是将#定义为注释标记更方便,就是这样,因此它被跳过了。