admin 管理员组文章数量: 1184232
这不起作用:
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint(0xFFFFFFFF))
]
它错误:
File "rtnetlink.py", line 243, in
class ifinfomsg(ctypes.Structure):
TypeError: Error when calling the metaclass bases
second item in _fields_ tuple (index 5) must be a C type
但是我可以在__init __()中设置值:
class ifinfomsg(ctypes.Structure):
_fields_ = [
('ifi_family', ctypes.c_ubyte),
('__ifi_pad', ctypes.c_ubyte),
('ifi_type', ctypes.c_ushort),
('ifi_index', ctypes.c_int),
('ifi_flags', ctypes.c_uint),
('ifi_change', ctypes.c_uint)
]
def __init__(self):
self.__ifi_pad = 0
self.ifi_change = 0xFFFFFFFF
这是通过__init__执行此操作的“正确”方法吗?
最佳答案 您的示例错误,因为ctypes.c_uint(0xFFFFFFFF)不是类型.在类定义中,一切都必须是一个类型(指针将是POINTER而不是指针).
我认为你使用的__init__方法就好了.我已经包装了一些非常大的结构,并且通常使用__init__,就像你设置默认值一样.
一般来说,如果你控制了你正在包装的C库,我认为你不应该在两个地方(C和Python)都有默认设置.在我访问C的一个大型库中,我创建了C函数“set_defaults”,我从__init__调用它.这样C只有用户才能访问默认值,并且只有一组代码需要维护.
本文标签: 默认值 ctypes python Structure structurepython
版权声明:本文标题:python ctypes structure_python – 设置ctypes.Structure默认值 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1755047568a3066687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论