创建枚举类
from enum import Enum
class VIP(Enum):
YELLOW = 1
RED = 2
BLUE = 3
查看枚举类型
print(VIP.YELLOW) #枚举类型
print(VIP['YELLOW']) #枚举类型
print(VIP.YELLOW.name) #枚举名称
print(VIP.YELLOW.value) #枚举值
print(VIP(3)) #数字得到枚举类型
for v in VIP: #遍历
print(v)
重复的枚举类型
class Shape(Enum):
SQUARE = 2
DIAMOND = 1
CIRCLE = 3
ALIAS_FOR_SQUARE = 2
- 当存在枚举成员的名称有重复时,则后面的枚举成员相当于第一个枚举成员的别名,而且在实际使用中,就是使用的第一次出现的枚举成员
for name, member in Shape.__members__.items():
print( name, member )