To read the values of the elements of a union in ctypes, create an instance of a ctypes.Union subclass and use attribute access, for example my_union.i. Every member reads the same bytes, so the value you get is only the one you stored if you read the member you wrote last. Reading any other member returns the same bits reinterpreted as that member's type.
This guide walks through a union with an integer, a float and a character, shows exactly what each read returns, and explains why. The output shown was produced with Python 3.12 on a little-endian 64-bit machine.
A union in ctypes is a class that inherits from ctypes.Union. The _fields_ list holds (name, type) pairs, exactly as for a ctypes.Structure.
The size is 4 bytes, the size of the largest member (c_int and c_float are both 4 bytes on common platforms). A new instance is zero-filled, so every member reads as zero.
When you write a member and read the same member back, you get the value you stored.
This is the safe pattern: write one member, read the same member. Everything else in this guide is about what happens when you read a different one.
Now read all three members after storing an integer, a float and then a character.
Each line shows the same four bytes read through three types.
i = 42, the float member shows a tiny denormal number, and c is b'*', which is byte 42 (the ASCII asterisk).f = 6.28, the float prints as 6.28000020980835 because a c_float cannot store 6.28 exactly. The integer is the float's bit pattern.c = b"A", only the first byte changes. The other three bytes still hold part of the old float, so the integer and the float are slightly different from before. Assigning a smaller member does not clear the rest of the union.If you want a clean state before writing a smaller member, create a new instance or call ctypes.memset(ctypes.addressof(u), 0, ctypes.sizeof(u)).
A c_char member returns a bytes object of length 1. Calling .decode("utf-8") works only when that byte is valid text. In the earlier example the byte after the float write is b'\xc3', which is not a complete UTF-8 character, so decoding it raises an error.
Decode only after you wrote text into the character member, or use ord(u.c) when you want the numeric byte value.
To see all the bytes that back every member, convert the instance to bytes. This is the clearest way to understand what the reads above return.
The integer printed by u.i matches int.from_bytes(raw, "little") on a little-endian machine. On a big-endian platform the byte order, and therefore the integer, would differ. That is another reason to treat cross-member reads as platform specific.
They are two views of the same four bytes. Writing the integer changes those bytes, and the float member reads the new bytes as an IEEE 754 float.
The union does not tell you. Keep the information yourself, usually as a separate tag field in a surrounding ctypes.Structure.
Yes, reads never modify the data. Only writes change the shared bytes, so the order of reads does not matter.
In a structure each field has its own storage, so reading one field never depends on writes to another. In a union they overlap, so a write to one member can change what every other member returns.
Read union values with attribute access on an instance of a ctypes.Union subclass. You get the stored value back only from the member you wrote last. Every other member shows the same bytes interpreted as its own type, a small write leaves the remaining bytes untouched, and bytes(u) shows exactly what the union holds.