Dan Dinh
Dan Dinh

Reputation: 8659

Cython: Linked List of Extension Type

I need a linked list using Cython extension type ie. cdef class but the Cython compiler complains about Python object.

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

Cython error: Pointer base type cannot be a Python object

It would be cdef struct but any work-around for using cdef class? (coz I need methods and OOP convention)

Upvotes: 1

Views: 192

Answers (1)

DavidW
DavidW

Reputation: 30936

cdef class is like any other Python object and is stored/passed by reference.

This means that it isn't necessary to use pointers here: the internal representation is already stored with pointers. Therefore just use cdef Item.

As with any other Python object, your Item will be reference counted and will be automatically reallocated when no other reference exists to it.

Upvotes: 2

Related Questions