python - 如何在 Python 中记录类型参数?

标签 python python-sphinx docstring type-variables

我想记录一下 T 在这个类中的含义

from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
    pass

我可以用 T.__doc__ = "Represents ..." 记录 T 但我可能想在几个不相关的地方使用 T类。

对于给定的类/函数等,是否有任何标准的方法来记录它?理想情况下使用 sphinx 和 reST。

我正在考虑与 sphinx 的 :param: 处于同一级别的东西,但是对于类型,沿着 scaladoc 的路线的 @tparam

最佳答案

您可以按照建议的方式通过多行字符串记录它:

from typing import Generic, TypeVar

T = TypeVar('T')

class A(Generic[T]):
"""
Description of the class and his input T

Inputs:
    T : what is T? the type of T

Returns:
    res 

"""

您可以将文档称为:

A.__doc__

help(A)

关于python - 如何在 Python 中记录类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836909/

相关文章:

python - Pandas:如何打开某些文件

python - Sphinx - 找不到语言 fr、cs 等的分词器

python - 单个模块/函数等的文档字符串超过 1 个?

python - PyCharm 中 Django 文档字符串中未解析的引用

python - 尝试为 Rail-RNA 创建 list 生成脚本 (python) 时输出错误。文件不匹配

python - 在文件中显式写入换行符

lightbox - 是否有 Sphinx 的灯箱扩展?

python - 将文档字符串添加到命名元组?

python - 使用 PuLP 的 MILP 近似解

python-sphinx - 自定义 globaltoc 和 localtoc html 渲染