【问题标题】:How can I document type parameters in Python?如何在 Python 中记录类型参数?
【发布时间】:2019-07-01 13:53:06
【问题描述】:

我想记录一下 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 的思路

【问题讨论】:

  • 您不能用多行字符串进行一般文档记录吗?
  • @BlueRineS 我不明白你的评论。你能详细说明一下吗?
  • 我的意思是像文档字符串或多行字符串。人们用于文档的常规内容。但是您可能需要更高级的东西?
  • 根据docs,你能说的就是TypeVar('T') # Can be anything——也就是说,它是通用的。 A 类派生自它,但可能要求或假定它具有某些特定属性。您可以在A 自己的文档中记录这些内容。

标签: python python-sphinx docstring type-variables


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2011-12-03
    • 2021-12-26
    • 2013-04-07
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    相关资源
    最近更新 更多