1
我正在處理一個Python腳本,用於打印結構中的所有值以及值名稱。結構中的值都是ctypes,但我無法打印它們。既然這樣,當我運行下面的代碼Python ctypes c_float.value不返回值
import ctypes
class test(ctypes.Structure):
pass
test._fields_ = [
('a', ctypes.c_float),
('b', ctypes.c_float),
('c', ctypes.c_float)]
d = test(1, 2, 3)
for field in d._fields_:
print field[0], field[1].value
我得到
a <attribute 'value' of '_ctypes._SimpleCData' objects>
b <attribute 'value' of '_ctypes._SimpleCData' objects>
c <attribute 'value' of '_ctypes._SimpleCData' objects>
有什麼想法?我以爲.value應該從ctypes對象中獲得價值,但它似乎並不想...
謝謝!
太棒了!完美的作品。回想起來:德哦。謝謝:) – ryantmer
您可以使用'__iter__'(yield值)和iteritems'(yield name,value tuples)的生成器方法定義一個'IterableStructure'子類。 – eryksun