Just call list(nums2)
. Example:
>>> from ctypes import *
>>> nums = [0,1,2,3]
>>> arr = (c_int*4)(*nums)
>>> arr
<__main__.c_long_Array_4 object at 0x00000217CE89F7C8>
>>> list(arr)
[0, 1, 2, 3]
Note this also works if your API returns a pointer as well, but needs string slicing since the total length must also be known:
>>> p = cast(arr,POINTER(c_int))
>>> p
<__main__.LP_c_long object at 0x00000217CE89F948>
>>> p.contents
c_long(0)
>>> p[0]
0
>>> p[3]
3
>>> p[4] # undefined behavior...past end of array.
0
>>> p[:4] # slice to known size
[0, 1, 2, 3]
FYI, as @user2357112 points out, slicing is ~4.5x faster:
In [1]: from ctypes import *
In [2]: nums = list(range(5000))
In [3]: arr = (c_int*5000)(*nums)
In [4]: %timeit list(arr)
454 μs ± 9.3 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [5]: %timeit arr[:]
107 μs ± 2.32 μs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…