先看一个枚举类:
from enum import Enumclass Color(Enum):YELLOW = 1GREEN = 2BLACK = 3RED = 4print(Color.GREEN.value) # 枚举值 取值 Color.GREEN.valueprint(Color.GREEN.name) # 枚举名称print(Color.GREEN) # 枚举类型# 枚举的名字和枚举类的区别print(type(Color.GREEN.name)) # 结果是字符串类型print(type(Color.GREEN)) # 结果是枚举下的一个类型print(Color['GREEN']) # 也能打印出枚举类型----------------------------------------------------输出:2GREENColor.GREEN<class 'str'><enum 'Color'>Color.GREEN
枚举也可以遍历的,能获取到每一种枚举类型
from enum import Enumclass Color(Enum):YELLOW = 1GREEN = 2BLACK = 3RED = 4for v in Color:print(v)------------------------------输出:Color.YELLOWColor.GREENColor.BLACKColor.RED