3

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

1 に答える 1

4

Cython の場合、A[1] は python オブジェクトになります。引き続き cdef を使用できるようにする場合は、 caller で自動キャストを使用します。

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

cython の -a モードでチェックして、各行のコードに Python を使用しているか C を使用しているかを確認できます。(cython -a yourfile.pyx -> は、参照して確認できる yourfile.html を生成します)。

于 2011-03-04T18:39:17.420 に答える