-2

I don't understand why the function compute works when it is outside of the the class myclass but doesn't works when it is inside?

import numpy as np
from numba import njit

@njit
def compute(length):
    x=np.zeros(length)
    for i in range(length):
        x[i] = i
    return x

class myclass():
    def __init__(self):
        self.length = 100

    def Simule(self):
        res = compute(self.length)
        print(res)

    def Simule2(self):
        res = self.compute(self.length)
        print(res)

    @njit
    def compute(self, length):
        x = np.zeros(length)
        for i in range(length):
            x[i] = i
        return x


if __name__ == "__main__":
    instance = myclass()
    instance.Simule()
    instance.Simule2()
ymmx
  • 4,769
  • 5
  • 32
  • 64
  • This was previosly answered by [How do I use numba on a member function of a class?](https://stackoverflow.com/questions/41769100/how-do-i-use-numba-on-a-member-function-of-a-class) – DarrylG Jan 24 '20 at 12:41
  • Does this answer your question? [How do I use numba on a member function of a class?](https://stackoverflow.com/questions/41769100/how-do-i-use-numba-on-a-member-function-of-a-class) – DarrylG Jan 24 '20 at 12:43

1 Answers1

1

It seems like this decorator doesn't recognize if a decorated callabe is function or method, you can change it to be a staticmethod:

import numpy as np
from numba import njit

@njit
def compute(length):
    x=np.zeros(length)
    for i in range(length):
        x[i] = i
    return x

class myclass():
    def __init__(self):
        self.length = 100

    def Simule(self):
        res = compute(self.length)
        print(res)

    def Simule2(self):
        res = self.compute(self.length)
        print(res)

    @staticmethod
    @njit
    def compute(length):
        x = np.zeros(length)
        for i in range(length):
            x[i] = i
        return x


if __name__ == "__main__":
    instance = myclass()
    instance.Simule()
    instance.Simule2()
marke
  • 1,024
  • 7
  • 20