3
从numba网站:使用Python类型提示与numba
from numba import jit
@jit
def f(x, y):
# A somewhat trivial example
return x + y
有没有办法有numba使用Python类型提示(如果有的话)?
从numba网站:使用Python类型提示与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
没有自动的方式了解注释并从这些注释中建立其签名。
既然是即时编译,则必须执行函数生成签名
In [119]: f(1.0,1.0)
Out[119]: 2.0
In [120]: f(1j,1)
Out[120]: (1+1j)
In [121]: f.signatures
Out[121]: [(float64, float64), (complex128, int64)]
每次以前不适合数据生成一个新的签名。