Next: 4.8 Mapping for Structures Up: 4. OMG IDL to Previous: 4.6 Mapping for Enumerations

# 4.7 Mapping for Unions

IDL unions map to Python classes with two attributes, the discriminant `d`, and the value `v`. The constructor of the class expects the discriminant and value as parameters (in that order). There are three possible states that a union can be in:

1.
  If the discriminant is explicitly listed in a case statement, then the value must be of the type associated with that case.
2.
  If the discriminant is not explicitly listed in a case statement and there is a default case, then the value must be of the type associated with the default case.
3.
  If the discriminant is not listed in case statement and there is no default case then the value must be the distinguished Python value, `None`.

e.g. considering each possible state in turn:

    module Example1 {
        union MyUnion switch(boolean) {
            true: string s;
            false: long n;
    };

    >>> import Example1
    >>> u = Example1.MyUnion(CORBA.TRUE, "Weh Hey")
    >>> u.d
    1
    >>> u.v
    `WeyHey'
    >>> u = Example1.MyUnion(CORBA.FALSE, 123)
    >>> u.d
    0
    >>> u.v
    123
    >>>

    module Example2 {
        union MyUnion switch(boolean) {
            true: string s;
            default: long n;
    };

    >>> import Example2
    >>> u = Example2MyUnion(CORBA.FALSE, 123)
    >>> u.d
    0
    >>> u.v
    123
    >>>

    module Example3 {
        union MyUnion switch(boolean) {
            true: string s;
    };

    >>> import Example3
    >>> u = Example.MyUnion(CORBA.FALSE, None)
    >>> u.d
    0
    >>> u.v
    None
    >>>