gpt4 book ai didi

python - 在 python 模块文档字符串中放入什么?

转载 作者:IT老高 更新时间:2023-10-28 12:25:32 25 4
gpt4 key购买 nike

好的,我已经阅读了 PEP 8PEP 257 ,并且我已经为函数和类编写了很多文档字符串,但是我有点不确定模块文档字符串中应该包含什么。我想,至少,它应该记录模块导出的函数和类,但我也看到了一些列出作者姓名、版权信息等的模块。有没有人有一个好的 python 文档字符串应该如何的例子结构化?

最佳答案

想想有人在交互式解释器的提示下执行 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)

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

我不知道诸如作者姓名和版权/许可之类的元数据如何帮助模块的用户——它可以放在评论中,因为它可以帮助考虑是否重用或修改模块的人。

关于python - 在 python 模块文档字符串中放入什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com