来自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
装饰器中提供:
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)]