How to use Python Class with Numba

2019-08-30 05:56发布

问题:

I have Numba 0.24 and it supports classes.

When I try to build the simplest class I can imagine I find an error! What's happening?

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'

Am I missing something here?

回答1:

You need to specify a spec:

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

bar = foo()
print bar.x

Take a look at the docs. At this point class variables are not supported. You have to use instance variables.