在cdef类中调用cdef

时间:2010-03-14 22:16:45

标签: python cython

是他们以任何方式完成这项工作,而不牺牲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 个答案:

答案 0 :(得分: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。)

相关问题