Enumeration types can be considered as labels or sets of constants, usually used to represent certain finite sets, such as weeks, months, states, etc.
There is no specific enumeration type in Python's Built-in types, but we can implement it in many ways, such as dictionaries, classes, etc.
MiracleLove = {'MON': 'Lin Zhiling', 'TUS': 'Chen Yihan', 'WEN': 'Cecilia Cheung', 'THU': 'Xin Zhilei', 'FRI': 'Weekly Winter Rain'} class MiracleLove: MON = 'Lin Zhiling' TUS = 'Chen Yihan' WEN = 'Cecilia Cheung' THU = 'Xin Zhilei' FRI = 'Weekly Winter Rain'
The above two methods can be considered as implementations of simple enumeration types.
It is not a problem if such enumerated variables are only used locally.
But the problem is that they are mutable, which means they can be modified elsewhere to affect their normal use.
What I don't know in the process of learning can be added to me? python learning communication deduction qun, 784758214 There are good learning video tutorials, development tools and e-books in the group. Share with you the current talent needs of python enterprises and how to learn python from zero foundation, and what to learn MiracleLove['MON'] = MiracleLove['FRI'] print(MiracleLove)
Enumerations defined by classes can even be instantiated, rendering them incompatible:
ml = MiracleLove() print(ml.MON) MiracleLove.MON = 2 print(ml.MON)
Of course, immutable types, such as tuples, can also be used, but this loses the intention of enumeration types and reduces labels to meaningless variables:
MiracleLove = ('R', 'G', 'B') print(MiracleLove[0], MiracleLove[1], MiracleLove[2])
To provide a better solution, Python added enum Standard Libraries in version 3.4 through PEP 435, and previous versions can download compatible libraries through pip install enum.
enum provides three tools, Enum/IntEnum/unique, which are also very simple to use. Enumeration types can be defined by inheriting Enum/IntEnum, where IntEnum defines that enumeration members must be (or can be converted to) integer types, while unique methods can be used as modifiers to limit the value of enumeration members to be non-repeatable:
from enum import Enum, IntEnum, unique try: @unique class MiracleLove(Enum): MON = 'Lin Zhiling' TUS = 'Chen Yihan' WEN = 'Cecilia Cheung' THU = 'Xin Zhilei' FRI = 'Weekly Winter Rain' except ValueError as e: print(e) # duplicate values found in <enum 'MiracleLove'>: FRI -> MON
try: class MiracleLove(IntEnum): MON = 1 TUS = 2 WEN = 3 THU = 4 FRI = 'Weekly Winter Rain' except ValueError as e: print(e) # invalid literal for int() with base 10:'Weekly Winter Rain'
More interestingly, Enum members are singletons and cannot be instantiated or changed:
class MiracleLove(Enum): MON = 'Lin Zhiling' TUS = 'Chen Yihan' WEN = 'Cecilia Cheung' THU = 'Xin Zhilei' FRI = 'Weekly Winter Rain' try: MiracleLove.MON = 2 except AttributeError as e: print(e) # Cannot reassign members.
Although not instantiatable, enumeration members can be assigned to variables:
mon = MiracleLove(0) tus = MiracleLove(1) wen = MiracleLove(2) print(mon, tus, wen) # MiracleLove.MON # MiracleLove.TUS # MiracleLove.WEN
A comparative judgement can also be made:
print(mon is MiracleLove.MON) print(mon == MiracleLove.MON) print(mon is tus) print(wen != MiracleLove.TUS) print(mon == 0) # Not equal to the value of any non-enumerated class # True # True # False # True # False
Finally, since enumeration members themselves are enumeration types, other members can also be found by enumeration members:
print(mon.TUS) print(mon.TUS.WEN.MON) # MiracleLove.TUS # MiracleLove.MON
However, this feature should be used with caution, because it may conflict with the name in the member's original namespace:
print(mon.name, ':', mon.value) class Attr(Enum): name = 'NAME' value = 'VALUE' print(Attr.name.value, Attr.value.name) # R : 0 # NAME value
If you are still confused in the world of programming, you can join our Python learning button qun: 784758214 to see how our predecessors learned! Exchange experience! I am a senior Python development engineer, from basic Python script to web development, crawler, django, data mining and so on, zero-based to the actual project data have been collated. To every Python buddy! Share some learning methods and small details that need attention. Click to join us. python learner gathering place
Summary:
The use of enum module is simple and its function is clear, but its implementation is worth learning. If you want to know more about the dark magic of Class and Metaclass in Python, and you don't know how to get started, you might as well read the source code of enum.