作者 :“大数据小禅”
枚举:在数学和计算机科学理论里,一个集的枚举列出某些有穷序列集的所有成员的程序
例子如下:
代码语言:javascript复制from enum import Enum
class traffictlight(Enum): #继承枚举类,枚举成员不可重复
RED=1 #枚举成员 RED是枚举的名字,1是枚举的值
YELLOW=2
GREEN=3
#枚举成员的比较 print(traffictlight.YELLOW.value == 1) 返回True
print(type(traffictlight.YELLOW))
print(traffictlight.YELLOW.name)
print(traffictlight.YELLOW.value)
print(traffictlight(2)) #通过枚举值获取枚举成员
def judge(color):
if color ==traffictlight.RED or color==traffictlight.YELLOW:
print("司机超速")
else:
print("正常行驶")
judge(traffictlight.GREEN)
#输出:
<enum'traffictlight'>
YELLOW
2
traffictlightYELLOW
正常行驶