numpy.char.chararray.ctypes#
attribute
- char.chararray.ctypes#
一个简化数组与ctypes模块交互的对象.
此属性创建一个对象,使在使用 ctypes 模块调用共享库时更容易使用数组.返回的对象具有 data,shape 和 strides 属性(请参见下面的注释)等属性,这些属性本身会返回可以用作共享库参数的 ctypes 对象.
- 参数:
- None
- 返回:
- cPython 对象
具有属性 data,shape,strides 等.
注释
以下是该对象的公共属性,已在“NumPy 指南”中进行了记录(我们省略了未记录的公共属性以及已记录的私有属性):
- _ctypes.data
指向数组内存区域的指针,表示为 Python 整数.此内存区域可能包含未对齐或字节顺序不正确的数据.该内存区域甚至可能不可写.将此属性传递给任意 C 代码时,应注意数组的标志和数据类型,以避免出现问题,包括 Python 崩溃.用户请注意!此属性的值与以下内容完全相同:
self._array_interface_['data'][0].请注意,与
data_as不同,不会保留对数组的引用:像ctypes.c_void_p((a + b).ctypes.data)这样的代码将导致指向已释放数组的指针,应写成(a + b).ctypes.data_as(ctypes.c_void_p)
- _ctypes.shape
(c_intpself.ndim): 长度为 self.ndim 的 ctypes 数组,其中 basetype 是与此平台上
dtype('p')对应的 C 整数(请参阅c_intp).此基本类型可以是ctypes.c_int,ctypes.c_long或ctypes.c_longlong,具体取决于平台.ctypes 数组包含底层数组的形状.
- _ctypes.strides
(c_intpself.ndim): 长度为 self.ndim 的 ctypes 数组,其中 basetype 与 shape 属性的 basetype 相同.此 ctypes 数组包含来自底层数组的 strides 信息.此 strides 信息对于显示必须跳转多少字节才能到达数组中的下一个元素非常重要.
- _ctypes.data_as(obj)[源代码]
返回数据指针,该指针强制转换为特定的 c-types 对象.例如,调用
self._as_parameter_等效于self.data_as(ctypes.c_void_p).也许你想使用该数据作为指向浮点数据 ctypes 数组的指针:self.data_as(ctypes.POINTER(ctypes.c_double)).返回的指针将保留对数组的引用.
- _ctypes.shape_as(obj)[源代码]
将 shape 元组作为某个其他 c-types 类型的数组返回.例如:
self.shape_as(ctypes.c_short).
- _ctypes.strides_as(obj)[源代码]
将 strides 元组作为某个其他 c-types 类型的数组返回.例如:
self.strides_as(ctypes.c_longlong).
如果 ctypes 模块不可用,则数组对象的 ctypes 属性仍然返回有用的信息,但不会返回 ctypes 对象,而是可能会引发错误.特别是,该对象仍然具有
as_parameter属性,该属性将返回一个等于 data 属性的整数.示例
>>> import numpy as np >>> import ctypes >>> x = np.array([[0, 1], [2, 3]], dtype=np.int32) >>> x array([[0, 1], [2, 3]], dtype=int32) >>> x.ctypes.data 31962608 # may vary >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) <__main__.LP_c_uint object at 0x7ff2fc1fc200> # may vary >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)).contents c_uint(0) >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint64)).contents c_ulong(4294967296) >>> x.ctypes.shape <numpy._core._internal.c_long_Array_2 object at 0x7ff2fc1fce60> # may vary >>> x.ctypes.strides <numpy._core._internal.c_long_Array_2 object at 0x7ff2fc1ff320> # may vary