最常见的 Python 文档字符串格式有哪些?
我见过几种不同的 Python 文档字符串编写风格,最流行的风格是什么?
I have seen a few different styles of writing docstrings in Python, what are the most popular styles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
格式
Python 文档字符串可以按照其他帖子所示的多种格式编写。但是,默认的 Sphinx 文档字符串格式并未提及,而是基于 reStructuredText (reST)。您可以在此博文中获取有关主要格式的一些信息。
请注意,reST 是 PEP 287 推荐的,
主要使用的格式如下文档字符串。
- Epytext
历史上类似 javadoc 的风格很流行,因此它被作为 Epydoc (使用称为
Epytext
格式)生成文档。示例:
- reST
如今,可能更流行的格式是 使用的 reStructuredText (reST) 格式Sphinx 生成文档。
注意:JetBrains PyCharm 中默认使用它(定义方法后键入三引号并按 Enter 键)。 Pyment 中也默认使用它作为输出格式。
示例:
- Google
Google 有自己的 格式经常使用的。它也可以由 Sphinx 解释(即使用 Napoleon 插件)。
示例:
甚至更多示例
- Numpydoc
请注意,Numpy 建议遵循他们自己的基于 Google 格式且可由 Sphinx 使用的 numpydoc。
转换/生成
可以使用像 Pyment 这样的工具自动为尚未记录的 Python 项目生成文档字符串,或将现有文档字符串(可以混合多种格式)从一种格式转换为另一种格式。
注意:这些示例取自 Pyment 文档
Formats
Python docstrings can be written following several formats as the other posts showed. However the default Sphinx docstring format was not mentioned and is based on reStructuredText (reST). You can get some information about the main formats in this blog post.
Note that the reST is recommended by the PEP 287
There follows the main used formats for docstrings.
- Epytext
Historically a javadoc like style was prevalent, so it was taken as a base for Epydoc (with the called
Epytext
format) to generate documentation.Example:
- reST
Nowadays, the probably more prevalent format is the reStructuredText (reST) format that is used by Sphinx to generate documentation.
Note: it is used by default in JetBrains PyCharm (type triple quotes after defining a method and hit enter). It is also used by default as output format in Pyment.
Example:
- Google
Google has their own format that is often used. It also can be interpreted by Sphinx (ie. using Napoleon plugin).
Example:
Even more examples
- Numpydoc
Note that Numpy recommend to follow their own numpydoc based on Google format and usable by Sphinx.
Converting/Generating
It is possible to use a tool like Pyment to automatically generate docstrings to a Python project not yet documented, or to convert existing docstrings (can be mixing several formats) from a format to an other one.
Note: The examples are taken from the Pyment documentation
Google 样式指南包含出色的 Python 样式指南。它包含可读文档字符串语法约定,提供更好的指导比 PEP-257 。例如:
我喜欢扩展它以在参数中包含类型信息,如 Sphinx 文档教程。例如:
The Google style guide contains an excellent Python style guide. It includes conventions for readable docstring syntax that offers better guidance than PEP-257. For example:
I like to extend this to also include type information in the arguments, as described in this Sphinx documentation tutorial. For example:
文档字符串约定位于 PEP-257 中,比 PEP-8 更详细。
然而,文档字符串似乎比其他代码区域更加个性化。不同的项目会有自己的标准。
我倾向于总是包含文档字符串,因为它们倾向于快速演示如何使用该函数及其功能。
我更喜欢保持一致,无论字符串的长度如何。我喜欢当缩进和间距一致时如何编码外观。这意味着,我使用:
Over:
并且倾向于在较长的文档字符串中省略对第一行的注释:
这意味着我发现像这样开始的文档字符串很混乱。
Docstring conventions are in PEP-257 with much more detail than PEP-8.
However, docstrings seem to be far more personal than other areas of code. Different projects will have their own standard.
I tend to always include docstrings, because they tend to demonstrate how to use the function and what it does very quickly.
I prefer to keep things consistent, regardless of the length of the string. I like how to code looks when indentation and spacing are consistent. That means, I use:
Over:
And tend to leave off commenting on the first line in longer docstrings:
Meaning I find docstrings that start like this to be messy.
显然没有人提到它:您还可以使用Numpy Docstring Standard。它在科学界被广泛使用。
拿破仑狮身人面像解析 Google 风格的文档字符串的扩展(在 @Nathan 的回答中推荐)也支持 Numpy 风格的文档字符串,并制作一个简短的 两者的比较。
最后一个基本示例可以让您了解它的外观:
As apparantly no one mentioned it: you can also use the Numpy Docstring Standard. It is widely used in the scientific community.
The Napolean sphinx extension to parse Google-style docstrings (recommended in the answer of @Nathan) also supports Numpy-style docstring, and makes a short comparison of both.
And last a basic example to give an idea how it looks like:
它是Python; 一切顺利。考虑如何发布您的文档。除了源代码的读者之外,文档字符串是不可见的。
人们真的很喜欢在网络上浏览和搜索文档。为此,请使用文档工具 Sphinx。它是记录 Python 项目的事实上的标准。该产品很漂亮 - 看看 https://python-guide.readthedocs.org/en /最新/。网站阅读文档将免费托管您的文档。
It's Python; anything goes. Consider how to publish your documentation. Docstrings are invisible except to readers of your source code.
People really like to browse and search documentation on the web. To achieve that, use the documentation tool Sphinx. It's the de-facto standard for documenting Python projects. The product is beautiful - take a look at https://python-guide.readthedocs.org/en/latest/ . The website Read the Docs will host your docs for free.
我建议使用 Vladimir Keleshev 的 pep257 Python 程序根据 PEP-257 和 Numpy 文档字符串标准,用于描述参数、返回等。pep257
将报告您与标准的差异,并像 pylint 和 pep8 一样调用。
I suggest using Vladimir Keleshev's pep257 Python program to check your docstrings against PEP-257 and the Numpy Docstring Standard for describing parameters, returns, etc.
pep257 will report divergence you make from the standard and is called like pylint and pep8.