V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bakabie
V2EX  ›  Python

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

  •  
  •   bakabie ·
    9bie · 2017-07-12 17:01:26 +08:00 · 2725 次点击
    这是一个创建于 2698 天前的主题,其中的信息可能已经有所发展或是发生改变。

    C 代码类似

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

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

    dbow
        1
    dbow  
       2017-07-12 17:24:30 +08:00   ❤️ 1
    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))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2675 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:59 · PVG 19:59 · LAX 03:59 · JFK 06:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.