python - "__docformat__"在 Python 中有什么用?

标签 python coding-style documentation pep

我已经使用 Python 编码大约一年了,我刚刚在 Theano tutorial 中遇到了一些代码。在文件顶部声明一个变量:

__docformat__ = 'restructedtext en'

网上搜索出this政治公众人物文件。虽然它仍然没有清楚地解释您何时/为什么要指定此变量。

我应该在所有 Python 代码的顶部声明它吗?

最佳答案

__docformat__ 的要点是允许 python 文档生成器工具,例如 epydoc了解如何正确解析模块文档(例如,期望使用哪种标记语言)。

来自epydoc docs :

To specify the markup language for a module, you should define a module-level string variable __docformat__, containing the name of the module's markup language. The name of the markup language may optionally be followed by a language code (such as en for English). Conventionally, the definition of the __docformat__ variable immediately follows the module's docstring

不过,epydoc等很多工具也支持通过命令行指定标记语言(例如epydoc --docformat restructuredtext),所以没有严格的必要包括文档格式。更重要的是您的项目或组织的编码约定。

关于python - "__docformat__"在 Python 中有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32217733/

相关文章:

python - 通过 int 转换存储在嵌套列表中的用户输入 - Python

python - 相当于 matlab dummyvar 的 numpy

python - 使用 Seaborn 将一维时间序列绘制为一条线,并沿 y 轴绘制边缘直方图

java - 重置计数器或让它增加并使用模数是否更有效

c++ - 在单独的函数或构造函数中初始化

vb.net - FxCop 或其他实用程序需要 VB.NET 中的内联文档?

swagger - 将 API Gateway Cloudformation 模板转换为 Swagger 文件

python - 为什么有时会保持既定秩序?

python - 如何在Python中实现类/对象元数据?

objective-c - Xcode 和 XIB 自文档