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

Diez B. Roggisch deets_noospaam at web.de
Mon Mar 8 11:36:09 EST 2004


chads wrote:

> 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;
> };

There is no direct equivalent to a union in python. The reason for this is
that python isn't as centered around memory-represantation as C is.

If you need a union-like structure to extract values from a memory-location,
you should look into the struct module.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list