To declare a C union in Python, subclass ctypes.Union and define _fields_ exactly as you would for a ctypes.Structure. The difference is memory: every field of a union starts at offset zero and shares the same bytes, so the union is as large as its biggest member and only one interpretation is meaningful at a time.
_fields__fields__fields_ is a list of (name, type) tuples. Instances start zero-filled, and you can set one field in the constructor:
The size is that of the largest field, here 4 bytes. Initializing more than one field in the constructor is legal but pointless, because the later value overwrites the earlier one.
Because all fields alias the same bytes, writing one changes the others. This is the classic way to inspect the bit pattern of a float:
Reading a different field from the one you last wrote is type punning. C permits it for unions, and in Python it is simply a reinterpretation of the bytes, so the result depends on the byte order and the sizes of the types involved.
C code usually pairs a union with a tag that says which member is valid. In ctypes, mark the union field as anonymous with _anonymous_, and its members become attributes of the outer structure:
_anonymous_ must be set before _fields_, and the name it lists must be a field that is a structure or union. The tag is not enforced by ctypes; your code has to check kind before it reads a member.
A plain Union uses the byte order of the machine. For data with a fixed byte order, such as a network packet or file header, use ctypes.BigEndianUnion or ctypes.LittleEndianUnion, available since Python 3.11:
To pass a union to a C function use it as any other ctypes type: pass it by value, or use ctypes.byref(u) for a pointer parameter, and set argtypes and restype on the function. Passing and returning unions by value works only where the platform ABI and your Python version support it, so test on every platform you target, or use a pointer.
| Aspect | ctypes.Structure | ctypes.Union |
|---|---|---|
| Field storage | Sequential, with padding | All fields at offset 0 |
| Size | Sum of fields plus padding | Size of the largest field |
| Independent fields | Yes | No, writes overlap |
| Typical use | Records, headers | Variants, bit-pattern views |
_pack_ with a union?Packing affects the alignment of fields in a structure. A union places every field at offset zero, so _pack_ has little practical effect except on the union's own alignment.
You cannot from the union alone. C programs store a separate tag, usually in an enclosing structure, and you have to read that tag yourself.
ctypes.sizeof() of my union look too large?The size is rounded up to the alignment of the most strictly aligned member. A union containing a c_double is 8 bytes or more even if the other fields are small.
typing.Union?No. typing.Union is a type hint meaning one of several Python types. ctypes.Union describes shared memory for interoperating with C.
Declare a union by subclassing ctypes.Union and listing _fields_. All fields overlap at offset zero, so writing one changes the others, and the size is that of the largest member. Wrap a union in a structure with a tag and _anonymous_ to model C variants, use BigEndianUnion or LittleEndianUnion for fixed byte order, and always check which member is valid before you read it.