python - 类定义中这些带引号的字符串是什么?
问题描述
我正在查看一些 Python 代码,这些代码以我以前从未见过的方式引用了类定义中使用的字符串:
class Foo:
"""
Docstring for the class. This one I understand, of
course. I'm wondering about the two strings below.
"""
one: str
"One thing."
another: str
"Another thing."
def __init__(self):
# etc, etc.
最后两个引用的字符串是做什么用的?我猜 Python 运行时会忽略它们,并且有一些外部工具使用它们?
解决方案
这些可能是为 Sphinx autodoc 准备的。Python 会忽略它们——Python 本身不支持属性的文档字符串——但 Sphinx 自动文档将属性定义之后的字符串文字识别为该属性的文档。我认为注释可能会用于此目的,但我没有设置来测试它。
推荐阅读
- angular - Angular 8 PWA 应用程序将 Server-Sent 事件永久置于挂起状态
- python - 将字符串拆分为长度连续增加的子字符串块
- wordpress - Wordpress 自定义模板不支持简码
- python - 使用 Selenium 获取 title= ' ' 中的信息
- python - 如何在 Python 中创建显式引用列表?
- intellij-idea - IntelliJ IDEA 中宏的位置
- reactjs - 在 Create-React-App 项目中动画萤火虫
- android - 上传多APK应用到华为应用市场?
- php - 从 PHP 数组中选择数字或数据
- python - 使用 Numpy 在 Python 中平均一组张量