简体   繁体   English

一组Python引用的双引号?

[英]A Single Set of Double Quotes for Python Comments?

While reviewing some code used in the online CS188.1x class on edX.org (code written by the instructors), I noticed the repeated use of a single set of double quotes (like one might use around a str) for use as comments. 在审查edX.org上的在线CS188.1x类中使用的一些代码(由教师编写的代码)时,我注意到重复使用一组双引号(如可能在str附近使用)用作注释。

I haven't seen this before, nor is it mentioned in PEP8 that I could find, but it certainly seems to work fine. 我之前没有见过这个,也没有在PEP8中提到我能找到的,但它似乎确实很好。 Can anyone enlighten me? 任何人都可以开导我吗?

Here's an example: 这是一个例子:

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 is any string literal that appears as the first statement in a class, method, function or module. docstring是任何字符串文字,它作为类,方法,函数或模块中的第一个语句出现。 Stylistically it's typical and preferred to use the triple quote format to allow for longer, better formatted docstrings, and to call attention to them for easy reference, but any string will qualify. 在风格上,使用三重引号格式以允许更长,更好格式化的文档字符串,并引起对它们的注意以便于参考,但是任何字符串都符合条件。

Docstrings are distinct from comments because comments are not relevant to the execution of a program at all, whereas docstrings are available at runtime by accessing an object's __doc__ variable. 文档字符串与注释不同,因为注释与程序的执行完全无关,而文档字符串在运行时通过访问对象的__doc__变量可用。

That's just docstring. 那只是文档。 It's a good design recommendation. 这是一个很好的设计建议。 For example, if you load that class in your python shell, you can call a help(bar) and you'll get "Here is another example of the comment style" 例如,如果你在python shell中加载该类,你可以调用一个help(bar) ,你会得到"Here is another example of the comment style"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM