python中的枚举

当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份:

JAN = 1

FEB = 2

MAR = 3

...

NOV = 11

DEC = 12

 

        甚至可以通过字典,或者建立一个类,通过属性名与属性值之间的对应实现枚举的功能,但是会有如下缺点:

1.枚举值可以修改

2.用类定义的话,这些枚举功能的类可以被实例化

3.枚举值是否重复无法检查

 

在Python3.4引入了enum模块,这个模块就是为了解决上面所说到的问题的,使用的时候首先引入模块:

import enum

enum里面提供了Enum和IntEnum等类,可以用来继承。当然,这些类就被限制了,

 

1.不能被继承

2.定义之后不能在类内修改

3.枚举名称不能重复。如果有值重复的情况,那么后者的名称将会是前者的别名,其实是一个对象。

 

要控制枚举值不能重复,可以在枚举类之前加上@unique,下面有例子。

 

        好处是简单,缺点是类型是int,并且仍然是变量。

        更好的方法是为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例。Python提供了Enum类来实现这个功能:

from enum import Enum

 

Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))

 

        这样我们就获得了Month类型的枚举类,可以直接使用Month.Jan来引用一个常量,或者枚举它的所有成员:

from enum import Enum
Month = Enum(
"Month",('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
# 或者
# Month = Enum("Month","Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")

for name,member in Month.__members__.items():
   
print(name, '=>', member, ',', member.value)
"""
Jan => Mont.Jan , 1
Feb => Mont.Feb , 2
Mar => Mont.Mar , 3
Apr => Mont.Apr , 4
May => Mont.May , 5
Jun => Mont.Jun , 6
Jul => Mont.Jul , 7
Aug => Mont.Aug , 8
Sep => Mont.Sep , 9
Oct => Mont.Oct , 10
Nov => Mont.Nov , 11
Dec => Mont.Dec , 12
"""

 

        value属性则是自动赋给成员的int常量,默认从1开始计数

 

        如果需要更精确地控制枚举类型,可以从Enum派生出自定义类:

from enum import Enum,unique

@unique
class Weekday(Enum):
    Sun =
0 # Sun的value被设定为0
   
Mon = 1
   
Tue = 2
   
Wed = 3
   
Thu = 4
   
Fri = 5
   
Sat = 6

for name,member in Weekday.__members__.items():
   
print(name,member,member.value)

 

from enum import Enum,unique

@unique
class Weekday(Enum):
    Sun =
0 # Sun的value被设定为0
   
Mon = 1
   
Tue = 2
   
Wed = 3
   
Thu = 4
   
Fri = 5
   
Sat = 6

for name,member in Weekday.__members__.items():
   
print(name,member,member.value)

"""
Sun Weekday.Sun 0
Mon Weekday.Mon 1
Tue Weekday.Tue 2
Wed Weekday.Wed 3
Thu Weekday.Thu 4
Fri Weekday.Fri 5
Sat Weekday.Sat 6
"""

 

@unique装饰器可以帮助我们检查保证没有重复值。

 

访问这些枚举类型可以有若干种方法:

>>> day1 = Weekday.Mon

>>> print(day1)

Weekday.Mon

>>> print(Weekday.Tue)

Weekday.Tue

>>> print(Weekday['Tue'])

Weekday.Tue

>>> print(Weekday.Tue.value)

2

>>> print(day1 == Weekday.Mon)

True

>>> print(day1 == Weekday.Tue)

False

>>> print(Weekday(1))

Weekday.Mon

>>> print(day1 == Weekday(1))

True

>>> Weekday(7)

Traceback (most recent call last):

  ...

ValueError: 7 is not a valid Weekday

>>> for name, member in Weekday.__members__.items():

...     print(name, '=>', member)

...

Sun => Weekday.Sun

Mon => Weekday.Mon

Tue => Weekday.Tue

Wed => Weekday.Wed

Thu => Weekday.Thu

Fri => Weekday.Fri

Sat => Weekday.Sat

 

你可能感兴趣的:(python)