首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Numba递归方法在Numba类中的实现

Python Numba递归方法在Numba类中的实现
EN

Stack Overflow用户
提问于 2022-01-30 21:16:33
回答 1查看 53关注 0票数 0

我目前正在实现一个由Numba @jitcalss装饰器包装的Python类。我的问题是编写递归方法。我知道有一些方法可以作为迭代方法来编写递归方法,但是在我的例子中,我相信递归的使用可以帮助我编写一个更可跟踪的程序脚本。据我所见,Numba不直接支持在Numba类中声明的递归方法。下面的代码并不直接显示我的情况,但它等同于我的问题。当我运行代码时,Python引发的错误也在下面给出。欢迎任何建议/改进/帮助。

代码语言:javascript
复制
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)

错误如下:

代码语言:javascript
复制
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:
EN

回答 1

Stack Overflow用户

发布于 2022-01-30 22:17:45

这是不支持的。文档指出:

numba中的递归支持目前仅限于自递归,函数的显式类型注释。此限制来自于无法确定递归调用的返回类型。

问题是不能指定函数的类型,因为它依赖于包含函数的self .

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70919008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档