为什么 njit 函数在 class 内部时不起作用,但在 class 外部时有效?

Why a njit function doesn't works when it's inside a class but work when it's outside?

我不明白为什么函数 compute 在 class myclass 外部时有效,但在 myclass 内部时却不起作用?

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()

这个装饰器好像不能识别被装饰的callabe是函数还是方法,你可以把它改成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()