【问题标题】:How to use Python Class with Numba如何在 Numba 中使用 Python 类
【发布时间】:2016-06-17 00:19:53
【问题描述】:

我有 Numba 0.24,它支持类。

当我尝试构建最简单的类时,我可以想象我发现了一个错误!发生了什么事?

from numba import jitclass
@jitclass
class foo:
    x = 2
bar = foo()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-3e0fd8d4bd2b> in <module>()
      3 class foo:
      4     x = 2
----> 5 bar = foo()

TypeError: wrap() missing 1 required positional argument: 'cls'

我错过了什么吗?

【问题讨论】:

    标签: python class numba


    【解决方案1】:

    您需要指定规格:

    spec = [('x', nb.int64)]
    @nb.jitclass(spec)
    class foo(object):
        def __init__(self):
            self.x = 2
    
    bar = foo()
    print bar.x
    

    看看docs。此时不支持类变量。你必须使用实例变量。

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2018-07-26
      • 1970-01-01
      • 2017-07-07
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2021-05-16
      相关资源
      最近更新 更多