python - 在 numba 中使用 python 类型提示

标签 python types numba

来自 numba 网站:

from numba import jit

@jit
def f(x, y):
    # A somewhat trivial example
    return x + y

有没有办法让 numba 使用 python 类型提示(如果提供)?

最佳答案

是也不是。您可以简单地使用普通的 python 语法进行注释(jit 装饰器将保留它们)。基于您的简单示例:

from numba import jit

@jit
def f(x: int, y: int) -> int:
    # A somewhat trivial example
    return x + y

>>> f.__annotations__
{'return': int, 'x': int, 'y': int}
>>> f.signatures  # they are not recognized as signatures for jit
[]

然而,要显式(强制执行)签名,它必须在 jit-decorator 中给出:

from numba import int_

@jit(int_(int_, int_))
def f(x: int, y: int) -> int:
    # A somewhat trivial example
    return x + y

>>> f.signatures
[(int32, int32)]  # may be different on other machines

据我所知,jit 没有自动方式来理解注释并从中构建其签名。

关于python - 在 numba 中使用 python 类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310278/

相关文章:

python - BashOperator 为其他 PythonOperators 中使用的库引发 ImportError

python - 从列表列表中提取部分重复项;返回单个匹配项以及每个重复项的来源记录; Python

python - 有没有更好的方法来迭代集合(A,B,C)选择(a,b,c)

c++ - 对象 vector 无法编译

python - 使用 Numba 支持组装 block 矩阵

python - xml.etree.ElementTree.ParseError : not well-formed (invalid token) due to "<" symbol in script

c - 如何在可移植裸机软件中使用嵌入式 C 语言中的类型

python-2.7 - 对列表列表运行计算的最快方法

python - np.mean 的 Numba 失败

mysql - MySQL ALTER TABLE 会重新格式化字段数据吗?