【发布时间】:2022-01-23 23:17:13
【问题描述】:
我正在使用numba.jitclass() 装饰器来编译一个python 类Honda,它将另一个python 类Car 作为输入参数:
class Car(object):
cc = '10'
class Honda(object):
def __init__(self):
self.car = Car()
def cal(self):
'Do heavy calculations'
Numba 无法识别 python 对象(类)的类型!
object = Car()
numba.typeof(object) # Error (not numba type)
试用 _ 1(不工作)
from numba.experimental import jitclass
from numba import types, jit, njit
@jitclass([('car', types.pyobject)]) # I tried to add `nopython=False`, but raise error
class Honda(object):
def __init__(self):
self.car = Car()
@jit
def cal(self): # TypeError: class members are not yet supported
'Do heavy calculations'
试用 _ 2(不工作)
@jitclass([('car', types.pyobject)]) # I tried to add `nopython=False`, but raise error
class Honda(object):
def __init__(self):
self.car = typed.pyobject(Car())
@jit
def cal(self): # TypeError: class members are not yet supported
'Do heavy calculations'
试用 _ 3(不工作)
@jitclass()
class Honda(object, Car):
def __init__(self):
pass
@jit
def cal(self): # TypeError: class members are not yet supported
'Do heavy calculations'
如何在由 numba.jitclass() 编译的类中简单地使用任何 python 对象(任何类类型)?并添加要使用的类成员(@njit 方法)?
有没有比numba.jitclass() 更好的方法来实现这一点?
【问题讨论】:
-
如何创建一个单独的函数来执行繁重的计算(在类范围之外),然后在实例方法中调用它?
-
谢谢,是的,我正在尝试使用单独的函数