- Extension type
- “Extension type” can refer to either a Cython class defined with
cdef class
or more generally to any Python type that is ultimately implemented as a native C struct (including the built-in types like int or dict). - Dynamic allocation
- Heap allocation
- A C variable allocated with
malloc
(in C) ornew
(in C++) is allocated dynamically/heap allocated. Its lifetime is until the user deletes it explicitly (withfree
in C ordel
in C++). This can happen in a different function than the allocation. - pointer
- A pointer is a variable that stores the address of another variable (i.e. direct address of the memory location). They allow for dynamic memory allocation and deallocation. They can be used to build dynamic data structures. Read more.
- Python object
- When using Python, the contents of every variable is a Python object
(including Cython extension types). Key features of Python objects are that
they are passed _by reference_ and that their lifetime is _managed_ automatically
so that they are destroyed when no more references exist to them.
In Cython, they are distinct from C types, which are passed _by value_ and whose
lifetime is managed depending on whether they are allocated on the stack or heap.
To explicitly declare a Python object variable in Cython use
cdef object abc
. Internally in C, they are referred to asPyObject*
. - Stack allocation
- A C variable declared within a function as
cdef SomeType a
is said to be allocated on the stack. It exists for the duration of the function only. - Typed memoryview
- A useful Cython type for getting quick access to blocks of memory.
A memoryview alone does not actually own any memory.
However, it can be initialized with a Python object that supports the
buffer protocol (typically “array” types, for example a Numpy array).
The memoryview keeps a reference to that Python object alive
and provides quick access to the memory without needing to go
through the Python API of the object and its
__getitem__
/__setitem__
methods. For more information, see Typed Memoryviews.