#!/usr/bin/python3
# -*- coding: utf-8 -*-# @Time : 2018/6/15 14:47# @File : enum_demo1.pyfrom enum import Enum, uniqueMonth = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))@unique # 如果要限制定义枚举时,不能定义相同值的成员。可以使用装饰器@unique【要导入unique模块】 Sat1 = 6 会报错class Weekday(Enum): Sun = 0 # Sun的value被设定为0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6 # Sat1 = 6for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)# Jan => Month.Jan , 1# Feb => Month.Feb , 2# Mar => Month.Mar , 3# Apr => Month.Apr , 4# May => Month.May , 5# Jun => Month.Jun , 6# Jul => Month.Jul , 7# Aug => Month.Aug , 8# Sep => Month.Sep , 9# Oct => Month.Oct , 10# Nov => Month.Nov , 11# Dec => Month.Dec , 12for i in Weekday.__members__.items(): print(i, type(i))# ('Sun', <Weekday.Sun: 0>) <class 'tuple'># ('Mon', <Weekday.Mon: 1>) <class 'tuple'># ('Tue', <Weekday.Tue: 2>) <class 'tuple'># ('Wed', <Weekday.Wed: 3>) <class 'tuple'># ('Thu', <Weekday.Thu: 4>) <class 'tuple'># ('Fri', <Weekday.Fri: 5>) <class 'tuple'># ('Sat', <Weekday.Sat: 6>) <class 'tuple'># 1.1 定义枚举时,成员名称不允许重复# 1.2 默认情况下,不同的成员值允许相同。但是两个相同值的成员,第二个成员的名称被视作第一个成员的别名# 1.3 如果枚举中存在相同值的成员,在通过值获取枚举成员时,只能获取到第一个成员for i in Weekday: print(i, end=' ')print()# Weekday.Sun Weekday.Mon Weekday.Tue Weekday.Wed Weekday.Thu Weekday.Fri Weekday.Sat(Sat1 = 6 不显示)# 2. 枚举取值# 2.1 通过成员的名称来获取成员print('Weekday[\'Sun\']---------', Weekday['Sun'])# Weekday['Sun']--------- Weekday.Sun# 2.2 通过成员值来获取成员print('Weekday(2)---------', Weekday(2))# Weekday(2)--------- Weekday.Tue# 2.3 通过成员,来获取它的名称和值we = Weekday.Sunprint('we.name, we.value------------', we.name, we.value)# we.name, we.value------------ Sun 0# 3. 迭代器# 3.1 枚举支持迭代器,可以遍历枚举成员for i in Weekday: print(i, type(i))# Weekday.Sun <enum 'Weekday'># Weekday.Mon <enum 'Weekday'># Weekday.Tue <enum 'Weekday'># Weekday.Wed <enum 'Weekday'># Weekday.Thu <enum 'Weekday'># Weekday.Fri <enum 'Weekday'># Weekday.Sat <enum 'Weekday'># 3.2 如果枚举有值重复的成员,循环遍历枚举时只获取值重复成员的第一个成员class Color(Enum): red = 1 orange = 2 yellow = 3 green = 4 blue = 5 indigo = 6 purple = 7 red_alias = 1for color in Color: print(color)# Color.red# Color.orange# Color.yellow# Color.green# Color.blue# Color.indigo# Color.purple# 3.3 如果想把值重复的成员也遍历出来,要用枚举的一个特殊属性__members__class Color(Enum): red = 1 orange = 2 yellow = 3 green = 4 blue = 5 indigo = 6 purple = 7 red_alias = 1for color in Color.__members__.items(): print(color)# ('red', <Color.red: 1>)# ('orange', <Color.orange: 2>)# ('yellow', <Color.yellow: 3>)# ('green', <Color.green: 4>)# ('blue', <Color.blue: 5>)# ('indigo', <Color.indigo: 6>)# ('purple', <Color.purple: 7>)# ('red_alias', <Color.red: 1>)# 4 枚举比较# 4.1 枚举成员可进行同一性比较print(Weekday.Sun is Weekday.Sun) # True# 4.2 枚举成员可进等值比较print(Weekday.Sun == Weekday.Sun) # True# 4.3 枚举成员不能进行大小比较# print(Weekday.Sun > Weekday.Sun)# TypeError: '>' not supported between instances of 'Weekday' and 'Weekday''''枚举取值 总结:通过成员的名称来获取成员;通过成员值来获取成员;通过成员,来获取它的名称和值.注意: Enum 的成员均为单例(Singleton),并且不可实例化,不可更改.枚举是可以比较的:举成员可进行同一性比较,可进等值比较,不能进行大小比较.总结:Enum可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较,并且枚举有多种实现方法。'''