如何使用python的工会?

Jus*_*ter 9 python union ctypes

我刚刚开始使用python,我想知道如何用python定义联合(使用ctypes)?希望我通过ctypes支持工会是对的.例如,以下c代码是如何在python中的

struct test
{
char something[10];
int status;
};

struct test2
{
char else[10];
int status;
int alive;
};

union tests
{
struct test a;
struct test2 b;
};

struct tester
{
char more_chars[20];
int magic;
union tests f;
};
Run Code Online (Sandbox Code Playgroud)

Thx,如果其他人正在寻找相同的答案,则添加简单示例

from ctypes import *

class POINT(Structure):
    _fields_ = [("x", c_int),
                 ("y", c_int)]

class POINT_1(Structure):
    _fields_ = [("x", c_int),
                 ("y", c_int),
                 ("z",c_int)]

class POINT_UNION(Union):
    _fields_ = [("a", POINT),
                 ("b", POINT_1)]

class TEST(Structure):
    _fields_ = [("magic", c_int),
                 ("my_union", POINT_UNION)]

testing = TEST()
testing.magic = 10;
testing.my_union.b.x=100
testing.my_union.b.y=200
testing.my_union.b.z=300
Run Code Online (Sandbox Code Playgroud)

Ada*_*eld 5

看一下ctypes教程.你使用这个ctypes.Union类:

class test(ctypes.Structure):
    # ...
class test2(ctypes.Structure):
    # ...

class tests(ctypes.Union):
    _fields_ = [("a", test),
                ("b", test2)]
Run Code Online (Sandbox Code Playgroud)