Cython将一个类传递给另一个类

Cython将一个类传递给另一个类,cython,Cython,我想将一个类引用传递给另一个类,这样我就可以调用由于类之间的组合关系而传递的类上的方法 这个最小的例子失败了: cdef class Klass: TheOtherKlass(self) cdef class TheOtherKlass: def __init__(self, Klass): self.Klass = Klass 与 为什么?self隐式地是传递给类方法的第一个参数 cdef class TheOtherKlass: def __ini

我想将一个类引用传递给另一个类,这样我就可以调用由于类之间的组合关系而传递的类上的方法

这个最小的例子失败了:

cdef class Klass:
    TheOtherKlass(self)

cdef class TheOtherKlass:
    def __init__(self, Klass):
        self.Klass = Klass

为什么?

self隐式地是传递给类方法的第一个参数

cdef class TheOtherKlass:
    def __init__(self, Klass):
        self.Klass = Klass


cdef class Klass:

    cdef TheOtherKlass myklass

    def __init__(self):               #Here self is passed as the first argument
        myklass = TheOtherKlass(self) #So it exists within the scope of __init__
你的声明: 他者 不在方法内,因此self在该范围内未定义

在下面的示例中,您可以使用self作为参数调用构造函数TheOtherKlassself,只要您在类方法中

cdef class TheOtherKlass:
    def __init__(self, Klass):
        self.Klass = Klass


cdef class Klass:

    cdef TheOtherKlass myklass

    def __init__(self):               #Here self is passed as the first argument
        myklass = TheOtherKlass(self) #So it exists within the scope of __init__