Python garbage collector

Extending bindings to be aware of memory management can be pretty awesome—you can make the Python garbage collector handle all memory management for you! If you have a structure in C, you can use the hooks from Python to make it feel just like any Python type. Consider the following C structure:

typedef struct data {
  int val;
} data_t;

We can then write the Cython declaration of the C struct into cdata.pxd as follows:

cdef extern from "data.h":
    struct data:
        int val
    ctypedef data data_t

We have several Cython-specific hooks. The one we care about is __cinit__. It is called in a cdef class just before __init__. You cannot assume any self-initialization since __init__ has not been called. But it allows us to do some memory ...

Get Learning Cython Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.