CHOICE

The ASN.1 CHOICE type represents a union-like data structure where only one of the possible fields can be set at a time.

Example ASN.1 definition:

MyChoice ::= CHOICE {
    intValue   INTEGER,
    strValue   UTF8String,
    boolValue  BOOLEAN
}

generates the following Python class:

class MyChoice(_Asn1Type):
    intValue: int
    strValue: str
    boolValue: bool

    class PRESENT(enum.IntEnum):
        PR_NOTHING = 0
        PR_intValue = 1
        PR_strValue = 2
        PR_boolValue = 3

    @property
    def present(self) -> PRESENT: ...
    def __init__(
        self,
        intValue: int = ...,
        strValue: str = ...,
        boolValue: bool = ...,
    ) -> None: ...

In Python, the generated class for a CHOICE behaves similarly to a C union:

  • Only one field can hold a value at any time.

  • Setting a field automatically clears all other fields (sets them to None).

  • Accessing a field that is not currently set returns None instead of raising an exception.

  • The currently selected field is tracked via a special enumeration called PRESENT, accessible via the present attribute.

  • An instance of a CHOICE may be created empty, representing an invalid or unset state.

Conceptual Representation

class _Asn1ChoiceType

Represents an ASN.1 CHOICE type.

present

Holds the current active field indicator as a member of PRESENT.

__init__(self, /, **members: Any) None

Initializes the CHOICE instance.

  • Only one field may be set via keyword arguments.

  • If multiple fields are provided or none, the object may represent an invalid or unset state.

  • Setting a field clears all others.

<field_name>

Access or set the value of the named field.

  • Setting a field clears other fields.

  • Getting an unset field returns None.

See also

_Asn1ChoiceType concept description.