Python ctypes.sizeof(...) 总是 returns 0

Python ctypes.sizeof(...) always returns 0

根据我对 Python 的 ctypes 的理解,ctypes.sizeof(...) 应该 return 传入的结构的大小(以字节为单位),就好像使用 C 的一样sizeof 运算符。但是,我总是得到 0 作为结果:

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> class testStruct(ctypes.Structure):
...     _fields = [
...         ("testField", ctypes.c_uint*4)
...     ]
...
>>> ctypes.sizeof(testStruct)
0
>>> test = testStruct()
>>> ctypes.sizeof(test)
0

为什么会这样?

__fields你忘记加下划线了,应该是_fields_.

import ctypes
class testStruct(ctypes.Structure):
    # NOT just _fields:
    _fields_ = [
        ("testField", ctypes.c_uint*4)
    ]

print(ctypes.sizeof(testStruct))
test = testStruct()
print(ctypes.sizeof(test))

输出:

16
16