【发布时间】:2019-06-27 11:23:44
【问题描述】:
我想要一个函数在其 dtype 旁边包含 NumPy ndarray 的类型提示。
例如,使用列表,可以执行以下操作...
def foo(bar: List[int]):
...
...为了给出bar 必须是由int 组成的list 的类型提示。
不幸的是,这种语法会为 NumPy ndarray 抛出异常:
def foo(bar: np.ndarray[np.bool]):
...
> np.ndarray[np.bool]) (...) TypeError: 'type' object is not subscriptable
是否可以为np.ndarray 提供dtype 特定的类型提示?
【问题讨论】:
-
上次我回答了一个类型提示问题,我找不到太多 numpy 的具体信息
-
什么是
List。list小写是一个普通的 Python 函数/类型。 -
提供更完整的上下文 - 你在导入什么
typing?你是如何使用打字的?您找到并导入了哪些numpy特定类型? -
@hpaulj,
List是内置的 mypy 类型:mypy.readthedocs.io/en/stable/builtin_types.html#built-in-types
标签: python python-3.x numpy type-hinting python-typing