在查看在线 CS188.1x 中使用的一些代码时在 edX.org 上的类(class)(由讲师编写的代码)中,我注意到重复使用了一组双引号(就像有人可能在 str 周围使用)用作注释。
我以前没见过这个,PEP8中也没有提到我能找到,但它似乎确实工作正常。谁能赐教一下?
这是一个例子:
class SomeClass():
"""
Some docstring info, using standard 'triple double quotes'
"""
def __init__(self):
"This is the comment style to which I'm referring."
some.code = foo # Here's a normal inline comment
def bar(self, item):
"Here is another example of the comment style"
return wtf
最佳答案
A docstring是在类、方法、函数或模块中作为第一条语句出现的任何字符串文字。在文体上,使用三重引号格式是典型且首选的方式,以允许更长、格式更好的文档字符串,并引起对它们的注意以便于引用,但任何字符串都符合条件。
文档字符串与注释不同,因为注释与程序的执行根本无关,而文档字符串在运行时通过访问对象的 __doc__
变量可用。
关于python - Python 注释的一组双引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885168/