python-3.x - Python:指定类型的文档属性
问题描述
让我们考虑以下示例:
from typing import Union
import numpy as np
class MyClass:
def __init__(self):
self._x = None
@property
def x(self) -> Union[float, np.ndarray]:
if len(self._x) == 1:
return self._x[0]
else:
return self._x
@x.setter
def x(self, value: Union[float, list, np.ndarray]):
self._x = np.atleast_1d(value)
关于如何正确记录上述代码的任何建议?我正在使用Sphinx
和numpydoc
。
非常感谢!
编辑:我意识到我的问题不是很清楚,所以我要补充几句。x
基本上,主要问题是如何记录作为float, list or array
输入和输出的事实float or array
。我在一些例子中看到这应该是getter
唯一的,我不知道我是否应该添加关键字Parameters
,Returns
或者它是否是更好的方法,因为我没有找到足够的答案(或者我只是想念他们)。
解决方案
您可以__doc__
在最终属性上显式分配属性,例如:
class MyClass:
def __init__(self):
self._x = None
@property
def x(self) -> Union[float, np.ndarray]:
...
@x.setter
def x(self, value: Union[float, list, np.ndarray]):
...
x.__doc__ = """\
Documentation text goes here.
(you may want to call textwrap.dedent on this string)
"""
推荐阅读
- linux - 按大小降序从 Tar 存档中提取 3 个最小的文件
- omnet++ - RSU 在静脉中的簇
- android - Spinner 中的项目可见但 onItemSelected 不起作用
- c++ - 在 Xubuntu 19.04 中编译 c++ 是否应该与在 Ubuntu 18.04 中编译 c++ 不同
- javascript - 在 HTML5UP 主题“镜头”中使用视频
- c# - WPF - 在 ListView 中禁用按钮时的奇怪行为
- postgresql - 在 PostgreSQL 中检查组内的条件
- .htaccess - 我在typo3中找不到htaccess
- arrays - 将浮点数写为 bytearray 在 python 中产生换行符
- sql-server - 如何在 SQL Server 的函数中使用变量