Python 的原生类型中并不包含枚举类型。为了提供更好的解决方案,Python 经过 PEP 435 在 3.4 版本中添加了 enum
标准库。python
枚举类型能够看做是一种标签或是一系列常量的集合,一般用于表示某些特定的有限集合,例如星期、月份、状态等。在没有专门提供枚举类型的时候咱们是怎么作呢,通常就经过字典或类来实现:post
Color = { 'RED' : 1, 'GREEN': 2, 'BLUE' : 3, } class Color: RED = 1 GREEN = 2 BLUE = 3
这种来实现枚举若是当心翼翼地使用固然没什么问题,毕竟是一种妥协的解决方案。它的隐患在于能够被修改。学习
更好的方式是使用标准库提供的 Enum
类型,官方库值得信赖。3.4 以前的版本也能够经过 pip install enum
下载支持的库。简单的示例:code
from enum import Enum class Color(Enum): red = 1 green = 2 blue = 3
枚举成员有值(默承认重复),枚举成员具备友好的字符串表示:ip
>>> print(Color.red) Color.red >>> print(repr(Color.red)) <Color.red: 1> >>> type(Color.red) <Enum 'Color'> >>> isinstance(Color.green, Color) True
枚举类型不可实例化,不可更改。ci
定义枚举时,成员名不容许重复字符串
class Color(Enum): red = 1 green = 2 red = 3 # TypeError: Attempted to reuse key: 'red'
成员值容许相同,第二个成员的名称被视做第一个成员的别名 get
class Color(Enum): red = 1 green = 2 blue = 1 print(Color.red) # Color.red print(Color.blue) # Color.red print(Color.red is Color.blue)# True print(Color(1)) # Color.red 在经过值获取枚举成员时,只能获取到第一个成员
若要不能定义相同的成员值,能够经过 unique 装饰 源码
from enum import Enum, unique @unique class Color(Enum): red = 1 green = 2 blue = 1 # ValueError: duplicate values found in <enum 'Color'>: blue -> red
能够经过成员名来获取成员也能够经过成员值来获取成员:it
print(Color['red']) # Color.red 经过成员名来获取成员 print(Color(1)) # Color.red 经过成员值来获取成员
每一个成员都有名称属性和值属性:
member = Color.red print(member.name) # red print(member.value) # 1
支持迭代的方式遍历成员,按定义的顺序,若是有值重复的成员,只获取重复的第一个成员:
for color in Color: print(color)
特殊属性 __members__
是一个将名称映射到成员的有序字典,也能够经过它来完成遍历:
for color in Color.__members__.items(): print(color) # ('red', <Color.red: 1>)
枚举的成员能够经过 is
同一性比较或经过 ==
等值比较:
Color.red is Color.red Color.red is not Color.blue Color.blue == Color.red Color.blue != Color.red
枚举成员不能进行大小比较:
Color.red < Color.blue # TypeError: unorderable types: Color() < Color()
IntEnum
是 Enum
的扩展,不一样类型的整数枚举也能够相互比较:
from enum import IntEnum class Shape(IntEnum): circle = 1 square = 2 class Request(IntEnum): post = 1 get = 2 print(Shape.circle == 1) # True print(Shape.circle < 3) # True print(Shape.circle == Request.post) # True print(Shape.circle >= Request.post) # True
enum
模块功能很明确,用法也简单,其实现的方式也值得学习,有机会的话能够看看它的源码。