Working with Python arrays¶
Note
This page uses two different syntax variants:
Cython specific
cdef
syntax, which was designed to make type declarationsconcise and easily readable from a C/C++ perspective.Pure Python syntax which allows static Cython type declarations inpure Python code,followingPEP-484 type hintsandPEP 526 variable annotations.
To make use of C data types in Python syntax, you need to import the special
cython
module in the Python module that you want to compile, e.g.importcython
If you use the pure Python syntax we strongly recommend you use a recentCython 3 release, since significant improvements have been made herecompared to the 0.29.x releases.
Python has a builtin array module supporting dynamic 1-dimensional arrays ofprimitive types. It is possible to access the underlying C array of a Pythonarray from within Cython. At the same time they are ordinary Python objectswhich can be stored in lists and serialized between processes when usingmultiprocessing
.
Compared to the manual approach withmalloc()
andfree()
, thisgives the safe and automatic memory management of Python, and compared to aNumpy array there is no need to install a dependency, as thearray
module is built into both Python and Cython.
Safe usage with memory views¶
fromcython.cimports.cpythonimportarrayimportarraya=cython.declare(array.array,array.array('i',[1,2,3]))ca=cython.declare(cython.int[:],a)print(ca[0])
fromcpythoncimportarrayimportarraycdefarray.arraya=array.array('i',[1,2,3])cdefint[:]ca=aprint(ca[0])
NB: the import brings the regular Python array object into the namespacewhile the cimport adds functions accessible from Cython.
A Python array is constructed with a type signature and sequence ofinitial values. For the possible type signatures, refer to the Pythondocumentation for thearray module.
Notice that when a Python array is assigned to a variable typed asmemory view, there will be a slight overhead to construct the memoryview. However, from that point on the variable can be passed to otherfunctions without overhead, so long as it is typed:
fromcython.cimports.cpythonimportarrayimportarraya=cython.declare(array.array,array.array('i',[1,2,3]))ca=cython.declare(cython.int[:],a)@cython.cfuncdefoverhead(a:cython.object)->cython.int:ca:cython.int[:]=areturnca[0]@cython.cfuncdefno_overhead(ca:cython.int[:])->cython.int:returnca[0]print(overhead(a))# new memory view will be constructed, overheadprint(no_overhead(ca))# ca is already a memory view, so no overhead
fromcpythoncimportarrayimportarraycdefarray.arraya=array.array('i',[1,2,3])cdefint[:]ca=acdefintoverhead(objecta):cdefint[:]ca=areturnca[0]cdefintno_overhead(int[:]ca):returnca[0]print(overhead(a))# new memory view will be constructed, overheadprint(no_overhead(ca))# ca is already a memory view, so no overhead
Zero-overhead, unsafe access to raw C pointer¶
To avoid any overhead and to be able to pass a C pointer to otherfunctions, it is possible to access the underlying contiguous array as apointer. There is no type or bounds checking, so be careful to use theright type and signedness.
fromcython.cimports.cpythonimportarrayimportarraya=cython.declare(array.array,array.array('i',[1,2,3]))# access underlying pointer:print(a.data.as_ints[0])fromcython.cimports.libc.stringimportmemsetmemset(a.data.as_voidptr,0,len(a)*cython.sizeof(cython.int))
fromcpythoncimportarrayimportarraycdefarray.arraya=array.array('i',[1,2,3])# access underlying pointer:print(a.data.as_ints[0])fromlibc.stringcimportmemsetmemset(a.data.as_voidptr,0,len(a)*sizeof(int))
Note that any length-changing operation on the array object may invalidate thepointer.
Cloning, extending arrays¶
To avoid having to use the array constructor from the Python module,it is possible to create a new array with the same type as a template,and preallocate a given number of elements. The array is initialized tozero when requested.
fromcython.cimports.cpythonimportarrayimportarrayint_array_template=cython.declare(array.array,array.array('i',[]))cython.declare(newarray=array.array)# create an array with 3 elements with same type as templatenewarray=array.clone(int_array_template,3,zero=False)
fromcpythoncimportarrayimportarraycdefarray.arrayint_array_template=array.array('i',[])cdefarray.arraynewarray# create an array with 3 elements with same type as templatenewarray=array.clone(int_array_template,3,zero=False)
An array can also be extended and resized; this avoids repeated memoryreallocation which would occur if elements would be appended or removedone by one.
fromcython.cimports.cpythonimportarrayimportarraya=cython.declare(array.array,array.array('i',[1,2,3]))b=cython.declare(array.array,array.array('i',[4,5,6]))# extend a with b, resize as neededarray.extend(a,b)# resize a, leaving just original three elementsarray.resize(a,len(a)-len(b))
fromcpythoncimportarrayimportarraycdefarray.arraya=array.array('i',[1,2,3])cdefarray.arrayb=array.array('i',[4,5,6])# extend a with b, resize as neededarray.extend(a,b)# resize a, leaving just original three elementsarray.resize(a,len(a)-len(b))
API reference¶
Data fields¶
data.as_voidptrdata.as_charsdata.as_scharsdata.as_ucharsdata.as_shortsdata.as_ushortsdata.as_intsdata.as_uintsdata.as_longsdata.as_ulongsdata.as_longlongs# requires Python >=3data.as_ulonglongs# requires Python >=3data.as_floatsdata.as_doublesdata.as_pyunicodes
Direct access to the underlying contiguous C array, with given type;e.g.,myarray.data.as_ints
.
Functions¶
The following functions are available to Cython from the array module
@cython.cfunc@cython.exceptval(-1)defresize(self:array.array,n:cython.Py_ssize_t)->cython.int
cdefintresize(array.arrayself,Py_ssize_tn)except-1
Fast resize / realloc. Not suitable for repeated, small increments; resizesunderlying array to exactly the requested amount.
@cython.cfunc@cython.exceptval(-1)defresize_smart(self:array.array,n:cython.Py_ssize_t)->cython.int
cdefintresize_smart(array.arrayself,Py_ssize_tn)except-1
Efficient for small increments; uses growth pattern that deliversamortized linear-time appends.
@cython.cfunc@cython.inlinedefclone(template:array.array,length:cython.Py_ssize_t,zero:cython.bint)->array.array
cdefinlinearray.arrayclone(array.arraytemplate,Py_ssize_tlength,bintzero)
Fast creation of a new array, given a template array. Type will be same astemplate
. If zero isTrue
, new array will be initialized with zeroes.
@cython.cfunc@cython.inlinedefcopy(self:array.array)->array.array
cdefinlinearray.arraycopy(array.arrayself)
Make a copy of an array.
@cython.cfunc@cython.inline@cython.exceptval(-1)defextend_buffer(self:array.array,stuff:cython.p_char,n:cython.Py_ssize_t)->cython.int
cdefinlineintextend_buffer(array.arrayself,char*stuff,Py_ssize_tn)except-1
Efficient appending of new data of same type (e.g. of same array type)n
: number of elements (not number of bytes!)
@cython.cfunc@cython.inline@cython.exceptval(-1)defextend(self:array.array,other:array.array)->cython.int
cdefinlineintextend(array.arrayself,array.arrayother)except-1
Extend array with data from another array; types must match.
@cython.cfunc@cython.inlinedefzero(self:array.array)->cython.void
cdefinlinevoidzero(array.arrayself)
Set all elements of array to zero.