好了,我读过PEP 8和PEP 257,也写过很多函数和类的文档字符串,但我有点不确定模块文档字符串中应该包含什么。我认为,它至少应该记录模块导出的函数和类,但我也见过一些模块列出了作者姓名、版权信息等。谁有一个好的python文档字符串应该如何结构的例子?
想象一下有人在交互式解释器的提示下提供帮助(你的模块)——他们想知道什么?(就信息量而言,其他提取和显示信息的方法大致相当于提供帮助)。如果你在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)
如您所见,这些组件的文档字符串中已经包含了关于类(以及函数,不过这里没有显示)的详细信息;模块自己的文档字符串应该非常概括地描述它们(如果有的话),而不是集中在模块作为一个整体可以为您做什么的简明总结上,最好是一些经过文档测试的示例(就像函数和类理想情况下应该在它们的文档字符串中有经过文档测试的示例)。
我不知道像作者名字和版权/许可这样的元数据对模块的用户有什么帮助——它可以放在注释中,因为它可以帮助人们考虑是否重用或修改模块。
引用规格:
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.
推荐文章
- 把if-elif-else语句放在一行中?
- 我如何结合两个数据框架?
- 如何计数列表中唯一值的出现
- 为什么Pycharm的检查人员抱怨“d ={}”?
- 如何JSON序列化集?
- 在python中,年龄从出生日期开始
- 使用pip安装SciPy
- 在Python中,我应该如何测试变量是否为None, True或False
- 如何在Python中从毫秒创建datetime ?
- 如何解窝(爆炸)在一个熊猫数据帧列,成多行
- 如何使用pip安装opencv ?
- 在pip冻结命令的输出中“pkg-resources==0.0.0”是什么
- 格式y轴为百分比
- 熊猫连接问题:列重叠但没有指定后缀
- 为什么空字典在Python中是一个危险的默认值?