【发布时间】:2021-01-17 01:49:39
【问题描述】:
我可以在 Python 3 中做到这一点:
def foo(bar: int):
pass
所以bar 保证是int。
假设我有一个像这样的 np 数组:
bar = np.zeros(5, dtype=np.float32)
如何注释foo 方法以强制执行此类型?
【问题讨论】:
-
请务必阅读Do type annotations in Python enforce static type checking?。如果你想注释一个numpy数组,你可以通过
type(bar)找出它是什么类型。 -
啊,是的,我误解了这些注释。来自 TypeScript,我希望这会被强制执行......
标签: python python-3.x numpy-ndarray python-3.8