11-3 枚举类型、枚举名称与枚举值
# 11-3 枚举类型、枚举名称与枚举值
# Q1:如何获得每一个标签下面所对应的数值
from enum import Enum
class VIP(Enum):
YELLOW = 1
BLACK = 3
RED = 4
# 别名
class common():
YELLOE = 1
print(VIP.YELLOW.value)#通过点value的形式打印标签的对应值
# 打印结果如图1
# Q2:如何获得每一个值下面所对应的标签
from enum import Enum
class VIP(Enum):
YELLOW = 1
BLACK = 3
RED = 4
# 别名
class common():
YELLOE = 1
print(VIP.YELLOW.name)#通过点name的形式打印数值对应的标签
# 打印结果如图2
# 看看下面的打印类型
from enum import Enum
class VIP(Enum):
YELLOW = 1
BLACK = 3
RED = 4
# 别名
class common():
YELLOE = 1
print(type(VIP.YELLOW.name))#看看这两个的不同
print(type(VIP.YELLOW))
print(VIP['RED'])
#打印结果如图3和图5,第一个是字符串类型,第二个是枚举类型
# 枚举类型(通过名称可以获得枚举类型)、枚举的值(1,2,3,4)、枚举的名字(标签的名字)
# 枚举是可以用来遍历的
from enum import Enum
class VIP(Enum):
YELLOW = 1
BLACK = 3
RED = 4
# 别名
class common():
YELLOE = 1
for v in VIP:#for循环的遍历
print(v)
#打印结果如图4





网友评论