NumPy 类型提示某些东西既是数组又是 float32? [英] NumPy type hint that something is both an array and float32?
问题描述
如何输入提示,函数返回的值都是 NumPy 数组并保存 NumPy float32 数据?
How can I type hint that a value being returned by a function is both an NumPy array and holds NumPy float32 data?
我可以指定返回的值是一个数组:
I can specify that the returned value is an array using:
def func() -> np.ndarray:
...
然而,这并不强制知道它是一个 float32 数组.
However, this does not enforce the knowledge that it is a float32 array.
我可以使用以下命令指定返回值的类型为 float32:
I can specify that the returned value is of type float32 using:
def func() -> np.float32:
...
然而,这并不强制知道它是一个数组(而不是单一的标量值).
However, this does not enforce the knowledge that it is an array (as opposed to a singular scalar value).
有没有办法在返回类型中同时指定两者?
Is there a way I can get both specified in the return type?
推荐答案
这目前是不可能的,但却是一个流行的功能请求;请参阅此 GitHub 问题,其中包含有关进度和可能的解决方法的信息.
This is currently impossible but is a popular feature request; see this GitHub issue which contains information on progress and possible workarounds.
这篇关于NumPy 类型提示某些东西既是数组又是 float32?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!