我有以下代码:
class MyStruct(ctypes.Structure):
_fields_= [('id', ctypes.uint),
('perm', ctypes.uint)]
定义类后,我可以直接从缓冲区复制数据到我的字段上。
例如:
ms = MyStruct.from_buffer_copy("\xAA\xAA\xAA\xAA\x11\x11\x11\x11")
print ms.id, ms.perm
一切正常,在这里id将0xAAAAAAAAA and perm等于0x11111111.
现在,我尝试在实例化过程中执行相同的操作,代码如下:
class MyStruct(ctypes.Structure):
_fields_= [('id', ctypes.uint),
('perm', ctypes.uint)]
def __init__(self):
super(MyStruct, self).__init__()
self.from_buffer_copy("\xAA\xAA\xAA\xAA\x11\x11\x11\x11")
ms = MyStruct()
print ms.id, ms.perm
但我的代码出现以下语句的错误:
AttributeError:“MyStruct”对象没有属性“from_buffer_copy”
经过一番研究我发现from_buffer_copy
is a ctypes._CData
方法。在文档中我们可以读到_CData
类是非公开课 http://docs.python.org/library/ctypes.html#ctypes._CData.
所以这是我的问题。我想用from_buffer_copy
在构造函数中,但此时它看起来“不可调用”。你可以帮帮我吗 ?
预先感谢您的回复
问候
PS:我不想使用这种风格super(MyStruct,self).__init__(id=0x44444444,perm=0x11111111)
因为在我的真实代码中有很多关于我的争论fields多变的。