开发者

calling a cdef in a cdef class

开发者 https://www.devze.com 2022-12-22 18:18 出处:网络
is their any way to make this work, without sacrificing the cdef in cdef caller? (no use of cpdef either)

is their any way to make this work, without sacrificing the cdef in cdef caller? (no use of cpdef either)

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


For Cython A[1] will be a python object. If you want to be able to still use cdef, use automatic cast in your caller :

cdef public caller(self):
    cdef Agents agent
    print "caller"
    agent = A[1]
    agent.called()

You can check with the -a mode in cython to know if you are using Python or C for each lines code. (cython -a yourfile.pyx -> will generate a yourfile.html that you can browse & check).

0

精彩评论

暂无评论...
验证码 换一张
取 消