Numba supports classes similar to Python classes and extension types. Currently the methods have static signatures, but in the future they we will likely support multiple inheritance and autojitting methods.

One can refer to the extension type as a type by accessing its exttype attribute:

class MyExtension(object):

def create_ext(arg):
    return MyExtension(arg)

It is not yet possible to refer to the extension type in the class body or methods of that extension type.

An example of extension classes and their capabilities and limitations is shown below:

Example for extension classes.

Things that work:

    - overriding Numba methods in Numba (all methods are virtual)
    - inheritance
    - instance attributes
    - subclassing in python and calling overridden methods in Python
    - arbitrary new attributes on extension classes and objects
    - weakrefs to extension objects

Things that do NOT (yet) work:

    - overriding methods in Python and calling the method from Numba
    - multiple inheritance of Numba classes
        (multiple inheritance with Python classes should work)
    - subclassing variable sized objects like 'str' or 'tuple'

from numba import jit, void, int_, double

# All methods must be given signatures

class Shrubbery(object):
    @void(int_, int_)
    def __init__(self, w, h):
        # All instance attributes must be defined in the initializer
        self.width = w
        self.height = h

        # Types can be explicitly specified through casts
        self.some_attr = double(1.0)

    def area(self):
        return self.width * self.height

    def describe(self):
        print("This shrubbery is ", self.width,
              "by", self.height, "cubits.")
shrub = Shrubbery(10, 20)
print(shrub.width, shrub.height)
shrub.width = 30
print(shrub._numba_attrs._fields_) # This is an internal attribute subject to change!

class MyClass(Shrubbery):
    def newmethod(self):
        print("This is a new method.")

shrub2 = MyClass(30,40)

Previous topic

Primitive values and Interfacing with C

Next topic


This Page