How to implement a union (C data type) in Python?

Bob Ippolito bob at
Mon Mar 8 17:12:10 CET 2004

On 2004-03-08 10:53:59 -0500, lamote1 at (chads) said:

> How would one implement a union (C data type) in Python, given this
> simple example?
> union {
>    struct {
>       int size;
>       float time;
>    } var1;
>    struct {
>       char initial;
>       float time;
>    } var2;
> };

Why exactly do you want this?  Yes, it matters.

Since you haven't inserted padding manually, this isn't necessarily a 
simple example because it depends on the way the C compiler has decided 
to pack structures.  Behavior of this union is architecture and 
compiler-flag dependent..


More information about the Python-list mailing list