The following code creates three "constants" that can be used without the enum's class name before each one. Is there a way to do this to all members of the enum without having an explicit line of code for each member as shown here?
import enum
class Test(enum.Enum):
ONE = 1
TWO = 2
THREE = 3
ONE = Test.ONE
TWO = Test.TWO
THREE = Test.THREE
Basically, is there an efficient way of doing this for an enum with 20-30 members all at once?
I wouldn't recommend to do that as the name of enum shows belonging. For example it's intuitive that january is working month from the following code:
class WorkingMonth(enum.Enum):
JANUARY = "january"
FEBRUARY = "february"
...
# WorkingMonth.JANUARY
There's no point in skimping on symbols in this case. Moreover, this increases the chance of potential error if you have enums with different names but intersecting values:
class ServerFormats(enum.Enum):
MP3 = "mp3"
WAV = "wav"
class ClientFormats(enum.Enum):
MP3 = "mp3"
FLAC = "flac"
# ServerFormats.MP3 and ClientFormats.MP3 will conflict
In a word, I would say that this is rather bad practice.