MaskedArray.ctypes¶An object to simplify the interaction of the array with the ctypesmodule.
This attribute creates an object that makes it easier to use arrayswhen calling shared libraries with the ctypes module. The returnedobject has, among others, data, shape, and strides attributes (seeNotes below) which themselves return ctypes objects that can be usedas arguments to a shared library.
| Parameters: | None |
|---|---|
| Returns: | c : Python object
|
See also
numpy.ctypeslib
Notes
Below are the public attributes of this object which were documentedin “Guide to NumPy” (we have omitted undocumented public attributes,as well as documented private attributes):
Be careful using the ctypes attribute - especially on temporaryarrays or arrays constructed on the fly. For example, calling(a+b).ctypes.data_as(ctypes.c_void_p) returns a pointer to memorythat is invalid because the array created as (a+b) is deallocatedbefore the next Python statement. You can avoid this problem usingeitherc=a+b orct=(a+b).ctypes. In the latter case, ct willhold a reference to the array until ct is deleted or re-assigned.
If the ctypes module is not available, then the ctypes attributeof array objects still returns something useful, but ctypes objectsare not returned and errors may be raised instead. In particular,the object will still have the as parameter attribute which willreturn an integer equal to the data attribute.
Examples
>>>importctypes>>>xarray([[0, 1], [2, 3]])>>>x.ctypes.data30439712>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_long))<ctypes.LP_c_long object at 0x01F01300>>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_long)).contentsc_long(0)>>>x.ctypes.data_as(ctypes.POINTER(ctypes.c_longlong)).contentsc_longlong(4294967296L)>>>x.ctypes.shape<numpy.core._internal.c_long_Array_2 object at 0x01FFD580>>>>x.ctypes.shape_as(ctypes.c_long)<numpy.core._internal.c_long_Array_2 object at 0x01FCE620>>>>x.ctypes.strides<numpy.core._internal.c_long_Array_2 object at 0x01FCE620>>>>x.ctypes.strides_as(ctypes.c_longlong)<numpy.core._internal.c_longlong_Array_2 object at 0x01F01300>