1. Definition of Enumeration
- First, define the enumeration to import the enum module.
- The enumeration definition uses the class keyword to inherit the Enum class.
from enum import Enum
class Color(Enum):
red = 1
orange = 2
yellow = 3
green = 4
blue = 5
indigo = 6
purple = 7
Code analysis:
- In the above code, we define the color enumeration Color.
- The color enumeration has 7 members, namely Color.red, Color.orange, Color.yellow, etc.
- Each member has its own name and value. The name of the Color.red member is: red, and the value is: 1.
- The data type of each member is the enumeration to which it belongs. [*Note: The class defined by class is actually a type]
1.1 When defining an enumeration, duplicate member names are not allowed
from enum import Enum
class Color(Enum):
red = 1
red = 2
The above code cannot be executed. Prompt error: TypeError: Attempted to reuse key:'red'
1.2 By default, different member values are allowed to be the same. But for two members with the same value, the name of the second member is regarded as the alias of the first member
from enum import Enum
class Color(Enum):
red = 1
red_alias = 1
The members Color.red and Color.red_alias have the same value, then the name red_alias of the member Color.red_alias is regarded as the alias of the member Color.red name red.
1.3 If there are members with the same value in the enumeration, only the first member can be obtained when the enumeration member is obtained by value
from enum import Enum
class Color(Enum):
red = 1
red_alias = 1
print(Color(1))
The output result is: Color.red
1.4 If you want to restrict the definition of enumeration, you cannot define members with the same value. You can use the decorator @unique [to import the unique module]
from enum import Enum, unique
@unique
class Color(Enum):
red = 1
red_alias = 1
Re-execution will prompt an error: ValueError: duplicate values found in <enum'Color'>: red_alias -> red
2. Enumeration value
2.1 Get members by their name
Color['red']
2.2 Get members by member value
Color(2)
2.3 Get its name and value through members
red_member = Color.red
red_member.name
red_member.value
3. Iterator**
3.1 Enumeration supports iterators, which can traverse enumeration members
for color in Color:
print(color)
The output is all members of the enumeration. Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.indigo, Color.purple.
Use print(color.value) to print out the values of all members. 1 2 3 4 5 6 7
3.2 If the enumeration has members with duplicate values, only the first member of the members with duplicate values is obtained when looping through the enumeration
from enum import Enum
class Color(Enum):
red = 1
orange = 2
yellow = 3
green = 4
blue = 5
indigo = 6
purple = 7
red_alias = 1
for color in Color:
print(color)
The output results are: _Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.indigo, Color.purple. _But _Color.red_alias_ does not appear in the output result.
3.3 If you want to traverse the members with repeated values, you must use a special attribute of the enumeration __members__**
from enum import Enum
class Color(Enum):
red = 1
orange = 2
yellow = 3
green = 4
blue = 5
indigo = 6
purple = 7
red_alias = 1
for color in Color.__members__.items():
print(color)
输出结果:(‘red’, <Color.red: 1>)、(‘orange’, <Color.orange: 2>)、(‘yellow’, <Color.yellow: 3>)、(‘green’, <Color.green: 4>)、(‘blue’, <Color.blue: 5>)、
(‘indigo’, <Color.indigo: 6>)、(‘purple’, <Color.purple: 7>)、(‘red_alias’, <Color.red: 1>)
4. Enumeration comparison
4.1 Enumeration members can be compared for identity
Color.red is Color.red
The output result is: True
Color.red is not Color.blue
The output result is: True
4.2 Enumeration members can be compared for equal value
Color.blue == Color.red
The output result is: False
Color.blue != Color.red
The output result is: True
4.3 Enumeration members cannot be compared for size
Color.red < Color.blue
Error in output result: TypeError: unorderable types: Color() <Color()