在不牺牲 cdef 调用程序中的 cdef 的情况下,他们有什么方法可以使这项工作正常进行吗? (也没有使用 cpdef)
from array import *
from numpy import *
cdef class Agents:
cdef public caller(self):
print "caller"
A[1].called()
cdef called(self):
print "called"
A = [Agents() for i in range(2)]
def main():
A[0].caller()
最佳答案
对于 Cython,A[1] 将是一个 python 对象。如果您希望仍然能够使用 cdef,请在调用者中使用自动转换:
cdef public caller(self):
cdef Agents agent
print "caller"
agent = A[1]
agent.called()
您可以使用 cython 中的 -a 模式进行检查,以了解您对每一行代码使用的是 Python 还是 C。 (cython -a yourfile.pyx -> 将生成一个你可以浏览和检查的 yourfile.html)。
关于python - 在 cdef 类中调用 cdef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2444000/