在cdef类中调用cdef
有没有办法让这个工作,而不牺牲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()
1 个回答
4
对于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文件,你可以打开这个文件查看)。