I wish to know if this is the best way to combine two or more Python Enum classes into a final class that collects each Enum class and to have a more levels.
Consider the example of @EthanFurman in the page Python Enum combination
from enum import Enum, unique
from itertools import chain
@unique
class Letters(Enum):
A = 'a'
B = 'b'
C = 'c'
def __str__(self):
return str(self.value)
@unique
class Numbers(Enum):
ONE = 'one'
TWO = 'two'
THREE = 'three'
def __str__(self):
return str(self.value)
Etahn Furman suggests the following class of combination:
class CombinedEnum(Enum):
""" doc string """
_ignore_ = 'member cls'
cls = vars()
for member in chain(list(Letters), list(Numbers)):
cls[member.name] = member.value
def __str__(self):
return str(self.value)
if __name__ == '__main__':
print(str(CombinedEnum.A))
print(str(CombinedEnum.B))
print(str(CombinedEnum.C))
print(str(CombinedEnum.ONE))
print(str(CombinedEnum.TWO))
print(str(CombinedEnum.THREE))
However, the multi-level class I would like to create should access the values in the following way:
CombinedEnum.Letters.A
CombinedEnum.Letters.B
CombinedEnum.Letters.C
CombinedEnum.Numbers.ONE
CombinedEnum.Numbers.TWO
CombinedEnum.Numbers.THREE
Typically, the basic solution I use is to use a composition class where the variables are the various Enum classes.
class World:
""" doc string """
LETTERS = Letters
NUMBERS = Numbers