问题:在python模块docstring中放什么?[关闭]

好的,我已经阅读了PEP 8PEP 257,并且为函数和类编写了许多文档字符串,但是我不确定模块文档字符串中应该包含什么。我认为,至少它应该记录该模块导出的功能和类,但是我也看到了一些列出作者姓名,版权信息等的模块。有没有人举过一个很好的python docstring应该怎么做的例子。有条理?

Ok, so I’ve read both PEP 8 and PEP 257, and I’ve written lots of docstrings for functions and classes, but I’m a little unsure about what should go in a module docstring. I figured, at a minimum, it should document the functions and classes that the module exports, but I’ve also seen a few modules that list author names, copyright information, etc. Does anyone have an example of how a good python docstring should be structured?


回答 0

考虑一个help(yourmodule)在交互式口译员提示下做的人-他们知道什么?(其他提取和显示信息的方法在信息量方面大致相同help)。因此,如果您有x.py

"""This module does blah blah."""

class Blah(object):
  """This class does blah blah."""

然后:

>>> import x; help(x)

显示:

Help on module x:

NAME
    x - This module does blah blah.

FILE
    /tmp/x.py

CLASSES
    __builtin__.object
        Blah

    class Blah(__builtin__.object)
     |  This class does blah blah.
     |  
     |  Data and other attributes defined here:
     |  
     |  __dict__ = <dictproxy object>
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__ = <attribute '__weakref__' of 'Blah' objects>
     |      list of weak references to the object (if defined)

如您所见,这些组件的文档字符串中已经包含了有关类的详细信息(以及函数,尽管这里没有显示)。该模块自己的文档字符串应该非常简要地描述它们(如果有的话),而应该专注于该模块作为一个整体可以为您做什么的简要概述,理想情况下是带有一些经过文档测试的示例(就像函数和类在理想情况下应该在文档中经过文档测试的示例一样)他们的文档字符串)。

我看不到诸如作者姓名和版权/许可证之类的元数据如何帮助该模块的用户-而是可以添加注释,因为它可以帮助某些人考虑是否重用或修改该模块。

Think about somebody doing help(yourmodule) at the interactive interpreter’s prompt — what do they want to know? (Other methods of extracting and displaying the information are roughly equivalent to help in terms of amount of information). So if you have in x.py:

"""This module does blah blah."""

class Blah(object):
  """This class does blah blah."""

then:

>>> import x; help(x)

shows:

Help on module x:

NAME
    x - This module does blah blah.

FILE
    /tmp/x.py

CLASSES
    __builtin__.object
        Blah

    class Blah(__builtin__.object)
     |  This class does blah blah.
     |  
     |  Data and other attributes defined here:
     |  
     |  __dict__ = <dictproxy object>
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__ = <attribute '__weakref__' of 'Blah' objects>
     |      list of weak references to the object (if defined)

As you see, the detailed information on the classes (and functions too, though I’m not showing one here) is already included from those components’ docstrings; the module’s own docstring should describe them very summarily (if at all) and rather concentrate on a concise summary of what the module as a whole can do for you, ideally with some doctested examples (just like functions and classes ideally should have doctested examples in their docstrings).

I don’t see how metadata such as author name and copyright / license helps the module’s user — it can rather go in comments, since it could help somebody considering whether or not to reuse or modify the module.


回答 1

引用规格

脚本 (独立程序)的文档字符串应可用作其“使用情况”消息,在使用不正确或缺失的参数(或者可能使用“ -h”选项表示“帮助”)调用脚本时打印。这样的文档字符串应记录脚本的功能和命令行语法,环境变量和文件。使用情况消息可能非常详尽(几个屏幕已满),并且对于新用户而言,足以正确使用该命令,并且对于高级用户而言,它是对所有选项和参数的完整快速参考。

模块的文档字符串 通常应列出该模块导出的类,异常和函数(以及任何其他对象),并以每行的一行摘要显示。(这些摘要所提供的详细信息通常少于对象的文档字符串中的摘要行。)程序包的文档字符串(即,程序包__init__.py模块的文档字符串)也应列出由程序包导出的模块和子程序包。

的文档字符串 应总结其行为,并列出公共方法和实例变量。如果该类打算被子类化,并且具有子类的附加接口,则该接口应单独列出(在文档字符串中)。类构造函数的方法应记录在文档字符串中__init__。各个方法应由自己的文档字符串记录。

函数方法的文档字符串 是一个以句点结尾的短语。它将函数或方法的效果规定为命令(“执行此操作”,“返回该值”),而不是说明。例如,不要写“返回路径名…”。函数或方法的多行文档字符串应总结其行为并记录其自变量,返回值,副作用,引发的异常以及何时可以调用它的限制(所有这些均适用)。应该指出可选参数。应该记录关键字参数是否是接口的一部分。

To quote the specifications:

The docstring of a script (a stand-alone program) should be usable as its “usage” message, printed when the script is invoked with incorrect or missing arguments (or perhaps with a “-h” option, for “help”). Such a docstring should document the script’s function and command line syntax, environment variables, and files. Usage messages can be fairly elaborate (several screens full) and should be sufficient for a new user to use the command properly, as well as a complete quick reference to all options and arguments for the sophisticated user.

The docstring for a module should generally list the classes, exceptions and functions (and any other objects) that are exported by the module, with a one-line summary of each. (These summaries generally give less detail than the summary line in the object’s docstring.) The docstring for a package (i.e., the docstring of the package’s __init__.py module) should also list the modules and subpackages exported by the package.

The docstring for a class should summarize its behavior and list the public methods and instance variables. If the class is intended to be subclassed, and has an additional interface for subclasses, this interface should be listed separately (in the docstring). The class constructor should be documented in the docstring for its __init__ method. Individual methods should be documented by their own docstring.

The docstring of a function or method is a phrase ending in a period. It prescribes the function or method’s effect as a command (“Do this”, “Return that”), not as a description; e.g. don’t write “Returns the pathname …”. A multiline-docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated. It should be documented whether keyword arguments are part of the interface.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。