好了,我读过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.
推荐文章
- 如何在Django模板中获得我的网站的域名?
- 在django Forms中定义css类
- 如何在Python中scp ?
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?