【问题标题】:how ctypes bitfields really work?ctypes 位域如何真正工作?
【发布时间】:2016-08-03 07:48:50
【问题描述】:

在python中,使用ctypes,以下是合法的:

from ctypes import *
class POINT(Structure):
    _fields_ = [("x", c_int),
     ("y", c_int)]

p = POINT(10,20)
sum = p.x + p.y

但这是如何工作的?我的意思是 p.x 如何完全合法?什么样的技巧提供了这个? 谢谢。

【问题讨论】:

    标签: python bit-fields


    【解决方案1】:

    当您初始化Structure 类的子类实例时,它会读取_fields_ 并根据该列表中的字段名称关联属性。

    这一切都是在 C 中完成的,所以如果你想确切地看到它是如何完成的,你需要使用源代码,特别是 Struct_type 的定义和 _init_pos_args 函数 https://github.com/python/cpython/blob/master/Modules/_ctypes/_ctypes.c#L4021

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 2013-04-04
      • 2013-01-06
      • 2013-03-13
      • 2013-10-31
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多