请问下, Python 如何做到结构体内嵌结构体呢?

2017-07-12 17:01:26 +08:00
 bakabie

C 代码类似

struct IMAGE_NT_HEADERS {
    IMAGE_OPTIONAL_HEADER IMAGE_OPTIONAL_HEADER {
    }
}
struct IMAGE_OPTIONAL_HEADER{....}

_(:з)∠)_。。。Python 如何做到呢?用 ctypes 没想出来怎么做

2657 次点击
所在节点    Python
1 条回复
dbow
2017-07-12 17:24:30 +08:00
https://docs.python.org/2/library/ctypes.html#structures-and-unions

You can, however, build much more complicated structures. A structure can itself contain other structures by using a structure as a field type.

Here is a RECT structure which contains two POINTs named upperleft and lowerright:

>>>
>>> class RECT(Structure):
... _fields_ = [("upperleft", POINT),
... ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print rc.upperleft.x, rc.upperleft.y
0 5
>>> print rc.lowerright.x, rc.lowerright.y
0 0
>>>
Nested structures can also be initialized in the constructor in several ways:

>>>
>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/374838

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX