Cython:扩展类型链表

Cython: Linked List of Extension Type

我需要一个使用 Cython 扩展类型的链表,即。 cdef class 但 Cython 编译器抱怨 Python 对象。

cdef class Item:
    cdef Item* prev
    cdef Item* next

Cython 错误:Pointer base type cannot be a Python object

它会是 cdef struct,但是使用 cdef class 有任何解决方法吗? (因为我需要方法和 OOP 约定)

cdef class 与任何其他 Python 对象一样,是 stored/passed 引用。

这意味着这里没有必要使用指针:内部表示已经用指针存储了。因此只需使用 cdef Item.

与任何其他 Python 对象一样,您的 Item 将被引用计数并在没有其他引用存在时自动重新分配。