from enum import Enum# Method 1classColor(Enum): RED =1# Enum member: RED GREEN =2# Enum member: GREEN BLUE =3# Enum member: BLUEprint(Color.RED)# Output: Color.REDprint(Color.RED.name)# Output: REDprint(Color.RED.value)# Output: 1# Method 2Animal =Enum('Animal', 'CAT DOG BIRD')print(Animal.CAT)# Output: Animal.CATprint(Animal.CAT.name)# Output: CATprint(Animal.CAT.value)# Output: 1for i in Animal:print(i)'''Output: Animal.CATAnimal.DOGAnimal.BIRD'''
from enum import EnumclassColor(Enum): RED =1 BLUE =2 GREEN =3print(Color.RED.name)# Output: "RED"print(Color.RED.value)# Output: "1"
enumeration:Colorenumeration members:RED, BLUE, GREEN 成員的名稱按照慣例為大寫,因為它們被視為常數。每個 enumeration member 都有兩個重要的屬性:name 和 value。 name 屬性給出 member name (string),value 屬性給出成員的值。
Enum members, values
有兩個同名的 enum members 是無效的。但是,兩個 enum members 允許具有相同的值。 給定兩個具有相同值的 members A 和 B(並且 A 首先定義),member B 是 A 的別名。
PYTHON
import enumclassShape(enum.Enum): DIAMOND =1 SQUARE =2 CIRCLE =3 ALIAS_FOR_SQUARE =2print(Shape.SQUARE)# Output: Shape.SQUAREprint(Shape.ALIAS_FOR_SQUARE)# Output: Shape.SQUAREprint(Shape(2))# Output: Shape.SQUAREclassShape(enum.Enum): SQUARE =2 SQUARE =3# Output: # TypeError: 'SQUARE' already defined as 2
from enum import EnumclassColor(Enum): RED =1 GREEN =2 BLUE =3 YELLOW =4# Accessing value of Enum memberprint(Color.RED.value)# Output: 1print(Color.RED.name)# Output: RED# Accessing Enum member based on its valueprint(Color(1))# Output: Color.RED# Accessing Enum memberprint(Color.RED)# Output: Color.REDprint(Color['RED'])# Output: Color.RED# =========================================================print(repr(Color.RED))# Output: <Color.RED: 1>print(list(Color))# Output: [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>]print(list(reversed(Color)))# Output: [<Color.BLUE: 3>, <Color.GREEN: 2>, <Color.RED: 1>]print(dir(Color))# Output: ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__']print(len(Color))# Output: 3print(type(Color))# <class 'enum.EnumType'>print(type(Color.RED))# <enum 'Color'>print(Color.RED in Color)# Output: Trueprint(isinstance(Color.GREEN, Color))# Output: True# =========================================================print(Color.RED is Color.BLUE)# Output: Falseprint(Color.RED isnot Color.BLUE)# Output: Trueprint(Color.RED == Color.BLUE)# Output: Falseprint(Color.RED != Color.BLUE)# Output: True# Enum members are not integers print(Color.RED ==1)# Output: False#print(Color.RED < Color.BLUE) #print(Color.RED <= Color.BLUE) # =========================================================# Iterating Enumfor i in Color:print(i)'''Color.REDColor.GREENColor.BLUEColor.YELLOW'''for name, member in Color.__members__.items():print(name, member)'''RED Color.REDGREEN Color.GREENBLUE Color.BLUEYELLOW Color.YELLOW'''# Used for detailed programmatic accessprint([name for name, member in Color.__members__.items() if member.name !=None])# Output: ['RED', 'GREEN', 'BLUE', 'YELLOW']'''names = []for name, member in Color.__members__.items(): if member.name is not None: names.append(name)'''# =========================================================# Enumeration members are hashable# Use Enum members as dictionary keyscolor_dict ={ Color.RED:'It symbolizes love and anger.', Color.GREEN:'It symbolizes nature and tranquility.', Color.BLUE:'It symbolizes sea and sky.',}print(color_dict[Color.RED])# Output: 'It symbolizes love and anger.'# Use Enum members in setscolor_set ={Color.RED, Color.BLUE}print(Color.GREEN in color_set)# Output: Falseprint(Color.RED in color_set)# Output: True
Enum
Enum 是 enum enumerations 的 Base Class。
EnumType 負責在其他 enum Class 上設置正確的方法,包含: name, value, _generate_next_value_, __str__ 方法...。
from enum import IntEnum, EnumclassShape(IntEnum): CIRCLE =1 SQUARE =2classRequest(IntEnum): POST =1 GET =2print(Shape.CIRCLE ==1)# Output: Trueprint(Shape.CIRCLE == Request.POST)# Output: True# =========================================================classColor(Enum): RED =1 GREEN =2print(Shape.CIRCLE == Color.RED)# Output: False# =========================================================# IntEnum To intprint(int(Shape.CIRCLE))# Output: 1# IntEnum To indexprint(['a', 'b', 'c'][Shape.CIRCLE])# Output: "b"# IntEnum To len()print([i for i inrange(Shape.SQUARE)])# Output: [0, 1]