[英]Error type: range() integer end argument expected, got LP_c_ulong
I got this error when I try to use 'slots_num' as a parameter in range(): 当我尝试使用'slots_num'作为range()中的参数时出现此错误:
slots_num = pointer(c_uint32())
slots = pointer(c_uint32())
if self.mgetBusSlotsFunc(self.mf, slots_num, slots) != 0:
raise Exception("Failed to get slots")
print(devAddr)
for x in range(0, slots_num):
print(slots[x])
What I did wrong? 我做错了什么?
The range built-in function expects integers as its arguments , range内置函数期望整数作为其参数 ,
The arguments to the range constructor must be integers (either built-in int or any object that implements the index special method).
范围构造函数的参数必须是整数(内置int或实现索引特殊方法的任何对象)。
but you have provided a pointer instance. 但是您提供了一个指针实例。
>>> import ctypes
>>> p = ctypes.pointer(ctypes.c_uint32(2))
>>> for i in range(0, p):print(i)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'LP_c_uint' object cannot be interpreted as an integer
You need to dereference the pointer to get the corresponding value. 您需要取消引用指针以获取相应的值。
>>> d = p.contents.value
>>> d
2
>>> for i in range(0, d):print(i)
...
0
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.