Python does not have an enumerated type, so a Slice enumeration is emulated using a Python class: the name of the Slice enumeration becomes the name of the Python class; for each enumerator, the class contains an attribute with the same name as the enumerator. For example:
The generated Python class looks as follows:
Each instance of the class has a
value attribute providing the Slice value of the enumerator, and a
name attribute that returns its name. The
valueOf class method translates a Slice value into its corresponding enumerator, or returns
None if no match is found.
Given the above definitions, we can use enumerated values as follows:
As you can see, the generated class enables natural use of enumerated values. The
Fruit class attributes are preinitialized enumerators that you can use for initialization and comparison.
Note that the generated class also defines a number of Python special methods, such as
__str__ and rich comparison operators, which we have not shown. The rich comparison operators compare the Slice value of the enumerator, which is not necessarily the same as its ordinal value.
Suppose we modify the Slice definition to include a custom enumerator value:
We can use
valueOf to examine the Slice values of the enumerators: