本文简要介绍 python 语言中  numpy.ndarray.ctypes  的用法。
- 用法:- ndarray.ctypes
- 一个用于简化数组与 ctypes 模块交互的对象。 - 此属性创建一个对象,使在使用 ctypes 模块调用共享库时更容易使用数组。返回的对象除其他外,还具有数据、形状和步幅属性(请参阅下面的注释),它们本身返回可用作共享库的参数的 ctypes 对象。 - None:
 
- c: Python 对象
- 拥有属性数据、形状、步幅等。 
 
 - 参数:- 返回:- 注意:- 以下是记录在“Guide to NumPy” 中的该对象的公共属性(我们省略了未记录的公共属性以及记录的私有属性): - 指向数组内存区域的指针,作为 Python 整数。此内存区域可能包含未对齐的数据,或不正确的字节顺序。内存区域甚至可能不可写。将此属性传递给任意C-code 时应尊重此数组的数组标志和数据类型,以避免可能包括 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。 数据:- (c_intp*self.ndim):长度为 self.ndim 的 ctypes 数组,其中基本类型是与此平台上的 - dtype('p')对应的 C-integer(请参阅- c_intp)。此 base-type 可能是- ctypes.c_int、- ctypes.c_long或- ctypes.c_longlong,具体取决于平台。 ctypes 数组包含底层数组的形状。
 - _ctypes。 形状:- (c_intp*self.ndim):长度为 self.ndim 的 ctypes 数组,其中基本类型与形状属性相同。此 ctypes 数组包含来自底层数组的步幅信息。此步长信息对于显示必须跳转多少字节才能到达数组中的下一个元素非常重要。 
 - _ctypes。 大步前进:- 将数据指针返回到特定的c-types 对象。例如,调用 - self._as_parameter_等效于- self.data_as(ctypes.c_void_p)。也许您想将数据用作指向浮点数据的 ctypes 数组的指针:- self.data_as(ctypes.POINTER(ctypes.c_double))。- 返回的指针将保留对数组的引用。 
 - _ctypes。 data_as (对象 ):- 将形状元组作为其他 c-types 类型的数组返回。例如: - self.shape_as(ctypes.c_short)。
 - _ctypes。 shape_as (对象 ):- 将步幅元组作为其他 c-types 类型的数组返回。例如: - self.strides_as(ctypes.c_longlong)。
 - _ctypes。 strides_as (对象 ):- 如果 ctypes 模块不可用,那么数组对象的 ctypes 属性仍然会返回一些有用的东西,但不会返回 ctypes 对象并且可能会引发错误。特别是,该对象仍将具有 - as_parameter属性,该属性将返回一个等于数据属性的整数。- 例子:- >>> 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
相关用法
- Python numpy ndarray.copy用法及代码示例
- Python numpy ndarray.astype用法及代码示例
- Python numpy ndarray.flat用法及代码示例
- Python numpy ndarray.setflags用法及代码示例
- Python numpy ndarray.setfield用法及代码示例
- Python numpy ndarray.sort用法及代码示例
- Python numpy ndarray.real用法及代码示例
- Python numpy ndarray.strides用法及代码示例
- Python numpy ndarray.itemset用法及代码示例
- Python numpy ndarray.__class_getitem__用法及代码示例
- Python numpy ndarray.partition用法及代码示例
- Python numpy ndarray.transpose用法及代码示例
- Python numpy ndarray.flatten用法及代码示例
- Python numpy ndarray.resize用法及代码示例
- Python numpy ndarray.dtype用法及代码示例
- Python numpy ndarray.imag用法及代码示例
- Python numpy ndarray.dot用法及代码示例
- Python numpy ndarray.size用法及代码示例
- Python numpy ndarray.fill用法及代码示例
- Python numpy ndarray.item用法及代码示例
- Python numpy ndarray.nbytes用法及代码示例
- Python numpy ndarray.tobytes用法及代码示例
- Python numpy ndarray.view用法及代码示例
- Python numpy ndarray.shape用法及代码示例
- Python numpy ndarray.base用法及代码示例
注:本文由纯净天空筛选整理自numpy.org大神的英文原创作品 numpy.ndarray.ctypes。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
