2016-09-23 53 views
0

这不起作用:蟒蛇ctypes的阵列将不会返回正确

def CopyExportVars(self, n_export): 
    export_array = (ctypes.c_double * n_export)() 
    self.dll_handle.vs_copy_export_vars(ctypes.cast(export_array, ctypes.POINTER(ctypes.c_double))) 
    return export_array().contents 

我得到这个错误(n_export 3):

TypeError: 'c_double_Array_3' object is not callable 
+0

'回报export_array.contents'? –

+0

@ŁukaszRogalski没有parens,它抱怨没有'内容'的属性。 – Jiminion

+0

Geez,对不起,'export_array'是值,而不是指针。因为它是一个'ctypes'数组,所以你可以索引它来取消值,'export_array [0]'全部到'export_array [n_export-1]' –

回答

1

错误是相当自我explainatory。 export_array不是可调用的对象,但您尝试在函数的最后一行中调用它。此外,您尝试使用与指针相关的接口('.contents')从数组中检索值,而不是指向它的指针。

,使工作将​​结果数组转换为纯Python对象,并返回其最简单的方法:

def CopyExportVars(self, n_export): 
    export_array = (ctypes.c_double * n_export)() 
    self.dll_handle.vs_copy_export_vars(ctypes.cast(export_array, ctypes.POINTER(ctypes.c_double))) 
    return list(export_array)