根据Python documentation:
推荐的创建具体数组类型的方法是乘以
任何具有正整数的 ctypes 数据类型。 或者,您可以
子类这个类型并定义 length 和 type 类变量。
数组元素可以使用标准下标读写
切片访问;对于切片读取,结果对象本身不是
数组。
例子:
>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print ii
<c_long_Array_10 object at 0x...>
>>> for i in ii: print i,
...
1 2 3 4 5 6 7 8 9 10
>>>
所以,第一部分 ctypes.c_int * len(x) 创建了一个带有 len(x) 元素的数组类型:
In [17]: ctypes.c_int * 10
Out[17]: __main__.c_int_Array_10
In [18]: ctypes.c_int * 100
Out[18]: __main__.c_int_Array_100
类型创建后,你应该调用它并传递数组元素:
(ctypes.c_int * len(x))(*x)
# ^^^^
创建的数组类型接受可变数量的元素,所以,你应该expand list x using the *x form:
In [24]: x = [1, 2, 3]
In [25]: (ctypes.c_int * len(x))(*x)
Out[25]: <__main__.c_int_Array_3 at 0x7f0b34171ae8>
In [26]: list((ctypes.c_int * len(x))(*x))
Out[26]: [1, 2, 3]
In [27]: (ctypes.c_int * len(x))(*x)[1]
Out[27]: 2
您不能传递x,因为__init__ 需要整数:
In [28]: (ctypes.c_int * len(x))(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-ff45cb7481e4> in <module>()
----> 1 (ctypes.c_int * len(x))(x)
TypeError: an integer is required (got type list)