There is another way to store additional values for enumeration members. extended-enum - helps to do this
from dataclasses import dataclass
from extended_enum import BaseExtendedEnumValue, ExtendedEnum, EnumField
@dataclass(frozen=True)
class SomeExtendedEnumValue(BaseExtendedEnumValue):
other_value: int
class MultiAttr(ExtendedEnum):
RED = EnumField(SomeExtendedEnumValue(value=1, other_value=10))
GREEN = EnumField(SomeExtendedEnumValue(value=2, other_value=20))
BLUE = EnumField(SomeExtendedEnumValue(value=3, other_value=20))
>>> MultiAttr(SomeExtendedEnumValue(2, 20)) == MultiAttr.GREEN
True
>>> MultiAttr.GREEN.value
2
>>> MultiAttr.GREEN.extended_value
SomeExtendedEnumValue(value=2, other_value=20)
>>> MultiAttr.GREEN.extended_value.other_value
20
>>> MultiAttr(SomeExtendedEnumValue(2, 30)) == MultiAttr.GREEN
ValueError: SomeExtendedEnumValue(value=2, other_value=30) is not a valid MultiAttr
If you don't need to compare the other_value field, you can use field(compare=False).
In this case, the comparison will take place on the first field.
from dataclasses import dataclass, field
@dataclass(frozen=True)
class SomeExtendedEnumValue(BaseExtendedEnumValue):
other_value: int = field(compare=False)
>>> MultiAttr(SomeExtendedEnumValue(2, 30)) == MultiAttr.GREEN
True
__init__you should pass two arguments,2and20, but you are only passing one argument, a tuple(2, 20).MultiAttr(2, 20)gives an error. Any thoughts about second question?