我目前正在实现一个由Numba @jitcalss装饰器包装的Python类。我的问题是编写递归方法。我知道有一些方法可以作为迭代方法来编写递归方法,但是在我的例子中,我相信递归的使用可以帮助我编写一个更可跟踪的程序脚本。据我所见,Numba不直接支持在Numba类中声明的递归方法。下面的代码并不直接显示我的情况,但它等同于我的问题。当我运行代码时,Python引发的错误也在下面给出。欢迎任何建议/改进/帮助。
import numba
spec = []
@numba.experimental.jitclass(spec)
class basicClass(object):
def __init__(self):
pass
def factorial(self,x):
if x==1:
return 1
else:
return x*self.factorial(x-1)
# MAIN BELOW
class_object = basicClass()
class_object.factorial(5)错误如下:
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
Failed in nopython mode pipeline (step: nopython frontend)
compiler re-entrant to the same function signature
- Resolution failure for non-literal arguments:
None
During: resolving callee type: BoundFunction((<class 'numba.core.types.misc.ClassInstanceType'>, 'factorial') for instance.jitclass.basicClass#22839d9cf70<>)
During: typing of call at <ipython-input-7-ab7b9737001d> (16)
File "<ipython-input-7-ab7b9737001d>", line 16:
def factorial(self,x):
<source elided>
else:
return x*self.factorial(x-1)
^
- Resolution failure for non-literal arguments:
None
During: resolving callee type: BoundFunction((<class 'numba.core.types.misc.ClassInstanceType'>, 'factorial') for instance.jitclass.basicClass#22839d9cf70<>)
During: typing of call at <string> (3)
File "<string>", line 3:发布于 2022-01-30 22:17:45
https://stackoverflow.com/questions/70919008
复制相似问题