Python 为什么njit函数不';t在';它在一个类中,但在它';外面有什么?

Python 为什么njit函数不';t在';它在一个类中,但在它';外面有什么?,python,numba,Python,Numba,我不明白为什么函数compute在类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.leng

我不明白为什么函数
compute
在类
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()

这是先前的回答,这是否回答了你的问题?