An enumeration type can be thought of as a label or a set of constants, often used to represent some particular finite set, such as week, month, status, and so on.

There are no enumerated types in Python’s built-in types, but there are many ways to implement them, such as dictionaries, classes, etc. :

MiracleLove = {'MON': 'Lin Chi-ling'.'TUS': 'Chen Yihan'.'WEN': 'Cecilia Cheung'.'THU': 'Xin Zhi Lei'.'FRI': Zhou Dongyu}

class MiracleLove:
   MON = 'Lin Chi-ling'
   TUS = 'Chen Yihan'
   WEN = 'Cecilia Cheung'
   THU = 'Xin Zhi Lei'
   FRI = Zhou Dongyu
Copy the code

The above two methods can be thought of as simple implementations of enumerated types.

This is fine if such enumeration variables are used only locally.

The problem is that they are mutable, which means they can be modified elsewhere to affect their proper use:

MiracleLove['MON'] = MiracleLove['FRI']
print(MiracleLove)
Copy the code

Enumerations defined by a class can even be instantiated to become nondescribable:

ml = MiracleLove()
print(ml.MON)

MiracleLove.MON = 2
print(ml.MON)
Copy the code

It is possible to use immutable types, such as tuples, but this defeats the purpose of enumerated types and reduces labels to meaningless variables:

MiracleLove = ('R'.'G'.'B')
print(MiracleLove[0], MiracleLove[1], MiracleLove[2])
Copy the code

To provide a better solution, Python has added the enum standard library in version 3.4 through PEP 435, and compatible supported libraries can also be downloaded through PIP Install Enum prior to version 3.4.

Enum provides enum /IntEnum/unique tools, which can be used to define enum types by inheriting enum /IntEnum. IntEnum defines enumerators that must be (or can be converted to) integers. The unique method can be used as a decorator to limit the value of an enumerator to non-repetition:

from enum import Enum, IntEnum, unique

try:
   @unique
   class MiracleLove(Enum):
       MON = 'Lin Chi-ling'
       TUS = 'Chen Yihan'
       WEN = 'Cecilia Cheung'
       THU = 'Xin Zhi Lei'
       FRI = Zhou Dongyu
except ValueError as e:
   print(e)
   
# duplicate values found in <enum 'MiracleLove'>: FRI -> MON
Copy the code
try:
   class MiracleLove(IntEnum):
       MON = 1
       TUS = 2
       WEN = 3
       THU = 4
       FRI = Zhou Dongyu
except ValueError as e:
   print(e)

# invalid literal for int() with base 10:
Copy the code

More interesting is that Enum members are singletons and cannot be instantiated or changed:

class MiracleLove(Enum):
   MON = 'Lin Chi-ling'
   TUS = 'Chen Yihan'
   WEN = 'Cecilia Cheung'
   THU = 'Xin Zhi Lei'
   FRI = Zhou Dongyu

try:
   MiracleLove.MON = 2
except AttributeError as e:
   print(e)

# Cannot reassign members.
Copy the code

Although not instantiable, enumerators can be assigned to variables:

mon = MiracleLove(0)
tus = MiracleLove(1)
wen = MiracleLove(2)
print(mon, tus, wen)

# MiracleLove.MON 
# MiracleLove.TUS 
# MiracleLove.WEN
Copy the code

Can also be compared to judge:

print(mon is MiracleLove.MON)
print(mon == MiracleLove.MON)
print(mon istus) print(wen ! = MiracleLove.TUS) print(mon ==0) # does not equal the value of any class other than this enumeration

# True
# True
# False
# True
# False
Copy the code

Finally, since enumerators are themselves enumerated types, they can also be used to find other members:

print(mon.TUS)
print(mon.TUS.WEN.MON)

# MiracleLove.TUS
# MiracleLove.MON
Copy the code

Use this feature with caution, however, as it may conflict with names in the member’s existing namespace:

print(mon.name, ':', mon.value)

class Attr(Enum):
   name  = 'NAME'
   value = 'VALUE'

print(Attr.name.value, Attr.value.name)

# R : 0
# NAME value
Copy the code

Conclusion:

The enum module is simple to use and functional, but its implementation is well worth learning. If you want to learn more about the dark arts of Class and Metaclass in Python but don’t know where to start, read the source code for enum.

Follow the public account “Python Column” and reply to “Tencent Architecture Resources 1” in the background to get a full set of big data learning resources package organized by Tencent architects!