【发布时间】:2017-12-27 22:39:06
【问题描述】:
我有一个用于设备的 C 库,其中包含以下功能:
int GetDevInfo(int *devices);
在这种情况下,devices 是一个整数数组,可能会这样定义:
int devices[10]
该函数将通过硬件总线循环查找活动设备。当它找到它们时,它会将设备号放入devices[] 中的下一个可用位置,例如在扫描之前:
devices = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 和一个全局变量 DeviceCount 知道有 0 个活动变量。该函数发挥其魔力并决定设备 5 和 8 处于活动状态。那么:
devices = {5, 8, 0, 0, 0, 0, 0, 0, 0, 0} 和 DeviceCount 知道有 2 个。
我希望 Python 函数是这样的。
devices = list(range(64))
for i in range(0,len(devices)): # Set them all to 0
devices[i] = 0
DeviceCount = 0
# Now we'll update it
DeviceCount = myModule.GetDevInfo(DeviceCount, devices)
返回时,DeviceCount 可能设置为 2,设备如下所示:
[5, 8, 0, 0, 0, 0, 0, 0, 0, 0]
我已经为其他传入整数和传回字符串的包装器编写了代码,但我没有找到任何网络智慧来帮助我解决这个问题。我知道我是否会这样做:
def f(a, data):
a.append(data)
l = [1, 2, 3]
f(l, 4)
我得到了l=[1, 2, 3, 4],但如何使用 Python 包装器到 C 来获得这种效果并不明显。
【问题讨论】:
-
建议:让函数只返回一个列表,其中
len(that_list) == DeviceCount
标签: c python-3.x parameters wrapper