-1

这段代码有什么问题?

>>> import ctypes as ct
>>> class Uni(ct.Union):
...       __fields__ = [("sh",ct.c_short),("sh2",ct.c_short)]

>>> t = Uni()
>>> t.sh = 0x0102
>>> t.sh
258
>>> t.sh2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Uni' object has no attribute 'sh2'
4

1 回答 1

4

_fields_它用一个下划线拼写:

>>> import ctypes as ct
>>> class Uni(ct.Union):
...     _fields_ = [('sh', ct.c_short), ('sh2', ct.c_short)]
... 
>>> t = Uni()
>>> t.sh = 0x0102
>>> t.sh2
258
于 2013-07-14T13:14:11.973 回答