【发布时间】:2012-04-13 11:00:13
【问题描述】:
实际上,我正在尝试将 ctypes 数组转换为 python 列表并返回。
如果找到this thread。但它假设我们在编译时就知道类型。
但是是否可以检索元素的 ctypes 类型?
我有一个至少包含一个元素的 python 列表。我想做这样的事情
import ctypes
arr = (type(pyarr[0]) * len(pyarr))(*pyarr)
这显然不起作用,因为type() 不返回与 ctypes 兼容的类。但是即使列表包含直接从 ctypes 创建的对象,上面的代码也不起作用,因为它是该类型的对象实例。
有什么方法可以完成这个任务吗?
[编辑]
好的,这是适合我的代码。我正在使用它将输入参数从 comtypes 服务器方法转换为 python 列表并将值返回到数组指针:
def list(count, p_items):
"""Returns a python list for the given times represented by a pointer and the number of items"""
items = []
for i in range(count):
items.append(p_items[i])
return items
def p_list(items):
"""Returns a pointer to a list of items"""
c_items = (type(items[0])*len(items))(*items)
p_items = cast(c_items, POINTER(type(items[0])))
return p_items
如前所述,p_list(items) 至少需要一个元素。
【问题讨论】:
标签: python arrays list types ctypes