当我尝试导入以下 jitclass 时,我在字典上收到 KeyError:
from numba import jitclass, float64
spec = [('n', float64),
('w', float64),
('a', float64)]
@jitclass(spec)
class foo:
def __init__(self,n,w):
self.n = n
self.w = w
def foo2(self):
a = self.n*self.w
return a + 1.
我不明白错误从何而来。 numba 文档不足以解释事情。我需要所有变量都是类变量吗?
最佳答案
jitclass
需要继承object
:
from numba import jitclass, float64
spec = [('n', float64),
('w', float64),
('a', float64)]
@jitclass(spec)
class foo(object):
def __init__(self,n,w):
self.n = n
self.w = w
def foo2(self):
a = self.n*self.w
return a + 1.
文档中似乎没有提到这一点,但示例是这样编写的。
关于python - 在 numba jitclass 中声明变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516640/