如何记录带有参数的方法?
如何使用 Python 的文档字符串来记录带有参数的方法?
PEP 257 给出了这个例子:
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0: return complex_zero
...
这是大多数 Python 开发人员使用的约定吗?
Keyword arguments:
<parameter name> -- Definition (default value if any)
我期待一些更正式的东西,例如
def complex(real=0.0, imag=0.0):
"""Form a complex number.
@param: real The real part (default 0.0)
@param: imag The imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0: return complex_zero
...
环境:Python 2.7.1
How to document methods with parameters using Python's documentation strings?
PEP 257 gives this example:
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0: return complex_zero
...
Is this the convention used by most Python developers?
Keyword arguments:
<parameter name> -- Definition (default value if any)
I was expecting something a little bit more formal such as
def complex(real=0.0, imag=0.0):
"""Form a complex number.
@param: real The real part (default 0.0)
@param: imag The imaginary part (default 0.0)
"""
if imag == 0.0 and real == 0.0: return complex_zero
...
Environment: Python 2.7.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于文档字符串是自由格式的,因此它实际上取决于您用来解析代码以生成 API 文档的内容。
我建议熟悉 Sphinx 标记,因为它被广泛使用,并正在成为记录 Python 项目的事实上的标准,部分原因是优秀的 readthedocs.org 服务。 转述示例作为 Python 片段的 Sphinx 文档:
此标记支持 文档之间的交叉引用等。请注意,Sphinx 文档使用(例如)
:py:attr:
,而您在从源代码记录时可以只使用:attr:
。当然,还有其他工具来记录 API。有更经典的 Doxygen 它使用
\param
命令,但这些命令并不是像 Sphinx 那样专门设计用于记录 Python 代码。请注意,有一个 类似问题,其中 类似的答案在这里......
Since docstrings are free-form, it really depends on what you use to parse code to generate API documentation.
I would recommend getting familiar with the Sphinx markup, since it is widely used and is becoming the de-facto standard for documenting Python projects, in part because of the excellent readthedocs.org service. To paraphrase an example from the Sphinx documentation as a Python snippet:
This markup supports cross-referencing between documents and more. Note that the Sphinx documentation uses (e.g.)
:py:attr:
whereas you can just use:attr:
when documenting from the source code.Naturally, there are other tools to document APIs. There's the more classic Doxygen which uses
\param
commands but those are not specifically designed to document Python code like Sphinx is.Note that there is a similar question with a similar answer in here...
根据我的经验,numpy 文档字符串约定(PEP257超集)是最广泛遵循的约定,也受到工具的支持,例如 狮身人面像。
一个例子:
Based on my experience, the numpy docstring conventions (PEP257 superset) are the most widely-spread followed conventions that are also supported by tools, such as Sphinx.
One example:
约定:
工具:
更新:从 Python 3.5 开始,您可以使用 类型提示,这是一种紧凑的、机器可读的语法:
这种语法的主要优点是它是由语言定义的并且是明确的,因此像 PyCharm 这样的工具可以轻松地利用 它。
Conventions:
Tools:
Update: Since Python 3.5 you can use type hints which is a compact, machine-readable syntax:
The main advantage of this syntax is that it is defined by the language and that it's unambiguous, so tools like PyCharm can easily take advantage from it.
python 文档字符串是自由格式,您可以按照您喜欢的任何方式记录它。
示例:
现在,有一些约定,但 python 不强制执行其中任何约定。有些项目有自己的惯例。一些使用文档字符串的工具也遵循特定的约定。
python doc strings are free-form, you can document it in any way you like.
Examples:
Now, there are some conventions, but python doesn't enforce any of them. Some projects have their own conventions. Some tools to work with docstrings also follow specific conventions.
如果您计划使用 Sphinx 来记录您的代码,它能够通过其“签名”功能为您的参数生成格式良好的 HTML 文档。 http://sphinx-doc.org/domains.html#signatures
If you plan to use Sphinx to document your code, it is capable of producing nicely formatted HTML docs for your parameters with their 'signatures' feature. http://sphinx-doc.org/domains.html#signatures
正如这里的其他答案已经指出的那样,主流可能是 Sphinx 方式,以便您稍后可以使用 Sphinx 生成那些精美的文档。
话虽如此,我个人偶尔也会使用内联评论风格。
这里还有一个例子,内联记录了一些微小的细节:
好处(正如 @mark-horvath 已经在另一条评论中指出的那样)是:
现在,有些人可能会认为这种风格看起来“丑陋”。但我想说“丑”是一个主观词。更中性的说法是,这种风格不是主流,所以你可能会觉得不太熟悉,从而不太舒服。再次强调,“舒适”也是一个主观词。但重点是,上述所有好处都是客观的。如果你遵循标准方式,你就无法实现它们。
希望将来的某一天,会有一个文档生成器工具也可以使用这种内联样式。这将推动采用。
PS:这个答案源于我自己在认为合适的时候使用内联注释的偏好。我也使用相同的内联样式来记录字典。
The mainstream is, as other answers here already pointed out, probably going with the Sphinx way so that you can use Sphinx to generate those fancy documents later.
That being said, I personally go with inline comment style occasionally.
One more example here, with some tiny details documented inline:
The benefits (as @mark-horvath already pointed out in another comment) are:
Now, some may think this style looks "ugly". But I would say "ugly" is a subjective word. A more neutual way is to say, this style is not mainstream so it may look less familiar to you, thus less comfortable. Again, "comfortable" is also a subjective word. But the point is, all the benefits described above are objective. You can not achieve them if you follow the standard way.
Hopefully some day in the future, there will be a doc generator tool which can also consume such inline style. That will drive the adoption.
PS: This answer is derived from my own preference of using inline comments whenever I see fit. I use the same inline style to document a dictionary too.
Sphinx Python 3 键入最小可运行示例
只是为了使其他答案所说的更具体:
build.sh
conf.py
index.rst
main.pyrequirements.txt
out/index.html< 上的输出/code>:
如果我们删除:
键入类型将显示在函数签名上:
预输入等效内容
以下产生相同的效果输出为版本
autodoc_typehints = "description"
,但参数名称重复较多:梦想:参数文档字符串
我们必须为每个
重新输入参数名称,这很糟糕:参数参数名称:
。目前看来还没有好的解决办法。将来可能会通过一些方式解决这个问题:Sphinx 可以向参数添加
#:
文档注释。此 Sphinx 扩展已适用于实例属性,如下所述:如何显示实例sphinx 文档中的属性?那么为什么不给我们:
Python 最终可以真正原生支持参数文档字符串!
TODO 查找这些功能请求!
同样的愿望也提到: https://stackoverflow.com/a/39581355/895245
其他有用的打字相关需要知道的事情
在 Python 3.10、Ubuntu 22.10 上测试。
Sphinx Python 3 typing minimal runnable example
Just to make what other answers said more concrete:
build.sh
conf.py
index.rst
main.py
requirements.txt
Output on
out/index.html
:If we remove:
typing types show on the function signature instead:
Pre-typing equivalent
The following produces the same output as the version with
autodoc_typehints = "description"
, but with a bit more parameter name repetition:The dream: argument docstrings
It is bad that we have to retype argument names for every
:param argumentname:
. There doesn't seem to be a good solution to this currently. Some ways it might be solved in the future:Sphinx could add
#:
doc comments to parameters. This Sphinx extension already works for instance attributes as mentioned at: How to show instance attributes in sphinx doc?so why not give us:
Python could finally actually support argument docstrings natively!
TODO find feature requests for these!
Same desire also mentioned at: https://stackoverflow.com/a/39581355/895245
Other useful typing related things to know
Tested on Python 3.10, Ubuntu 22.10.
基于类型提示答案(https://stackoverflow.com/a/9195565/2418922),它提供了更好的结构化方式来记录参数类型,还存在一种结构化方式来记录参数的类型和描述:
示例来自:https://pypi.org/project/autocommand/
Building upon the type-hints answer (https://stackoverflow.com/a/9195565/2418922), which provides a better structured way to document types of parameters, there exist also a structured manner to document both type and descriptions of parameters:
example adopted from: https://pypi.org/project/autocommand/
文档字符串仅在交互式环境中有用,例如 Python shell。当记录不会交互使用的对象(例如内部对象、框架回调)时,您不妨使用常规注释。这是我用来将缩进注释挂在项目上的样式,每个注释都在自己的行上,这样您就知道该注释适用于:
您不能使用文档字符串执行此类操作。
Docstrings are only useful within interactive environments, e.g. the Python shell. When documenting objects that are not going to be used interactively (e.g. internal objects, framework callbacks), you might as well use regular comments. Here’s a style I use for hanging indented comments off items, each on their own line, so you know that the comment is applying to:
You can’t do this sort of thing with docstrings.