Skip to main content
 首页 » 编程设计

快速掌握Python枚举教程

2022年07月19日134youxin

Python3.4版本引入新的数据类型——枚举,本文带你学习完整的枚举类型。

枚举概述

枚举是一组绑定唯一常量值的符号名称。枚举用于场景简单的自定义类型,如季节、星期、游戏中的武器类型等,枚举命名一般约定为大写字母开头,且为单数形式。

Python中使用enum模块创建枚举,通过class关键字或函数方式创建枚举。enum有几个派生类,IntEnum,enum.IntFlag,enum.Flag。

简单枚举示例

首先我们看个简单示例:

from enum import Enum 
 
# 继承自基类Enum 
class Season(Enum): 
    SPRING = 1 
    SUMMER = 2 
    AUTUMN = 3 
    WINTER = 4 
 
# 访问成员 
seas = Season.SPRING 
print(seas) 
 
# 枚举值比较 
if seas == Season.SPRING: 
    print("Spring") 
 
# 通过list可以访问枚举所有可能的值 
print(list(Season)) 
 

上面示例我们定义Season枚举,包括四个值。通过枚举名称+点+成员名称来访问成员。

输出结果:

Season.SPRING 
Spring 
[<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>] 

第二个示例

我们再看一个实例,其中使用了枚举相关的基本函数。

from enum import Enum 
 
 
class Season(Enum): 
    SPRING = 1 
    SUMMER = 2 
    AUTUMN = 3 
    WINTER = 4 
 
 
seas = Season.SPRING 
 
# 以人可读的字符串形式输出枚举值 
print(seas) 
 
# isinstance方法检测变量释放为Season类型 
print(isinstance(seas, Season)) 
 
# type函数输出变量类型 
print(type(seas)) 
 
# repr函数输出枚举的更多信息 
print(repr(seas)) 
 
# 可以通过成员名称或索引访问枚举成员 
print(Season['SPRING']) 
print(Season(1)) 

输出结果:

Season.SPRING 
True 
<enum 'Season'> 
<Season.SPRING: 1> 
Season.SPRING 
Season.SPRING 

通过函数API方式创建枚举

Python枚举也可以通过API方式创建。

from enum import Enum 
 
# 有多种方式指定枚举项,这里使用字符串 
# 通过Enum函数创建枚举,通过字符串指定枚举项,start表示从1开始 
Season = Enum('Season', 'SPRING SUMMER AUTUMN WINTER', start=1) 
 
seas = Season.SUMMER 
print(seas) 
 
if seas == Season.SUMMER: 
    print("Summer") 

输出结果:

Season.SUMMER 
Summer 

迭代枚举

下面示例展示如何迭代枚举:

from enum import Enum 
 
# 通过字符串列表创建枚举 
Season = Enum('Season', ['SPRING', 'SUMMER', 'AUTUMN', 'WINTER'], start=5) 
 
for season in Season: 
    print(season) 
 
# 迭代枚举名称和值 
for season in Season: 
    print(season.name, season.value) 

输出结果:

Season.SPRING 
Season.SUMMER 
Season.AUTUMN 
Season.WINTER 
SPRING 5 
SUMMER 6 
AUTUMN 7 
WINTER 8 

自动赋值

Python枚举可以通过auto函数自动赋值:

from enum import Enum 
from enum import auto 
 
# auto函数自动赋值 
class Season(Enum): 
    SPRING = 1 
    SUMMER = auto() 
    AUTUMN = auto() 
    WINTER = auto() 
 
 
for season in Season: 
    print(season.value) 

输出结果:

1 
2 
3 
4 

枚举成员值的唯一性

枚举的成员值可以通过@unique装饰器确保其唯一性:

from enum import Enum, unique 
 
@unique 
class Season(Enum): 
    SPRING = 1 
    SUMMER = 2 
    AUTUMN = 3 
    WINTER = 3 
 
 
for season in Season: 
    print(season) 

运行报错:ValueError: duplicate values found in <enum ‘Season’>: WINTER -> AUTUMN

如果注释掉装饰器行,则会正确输出,但重复的WINTER被忽略了。

Season.SPRING 
Season.SUMMER 
Season.AUTUMN 

枚举的成员 __members__

from enum import Enum 
 
# 通过字典方式定义枚举 
Season = Enum('Season', [('SPRING', 1), ('SUMMER', 2),  
    ('AUTUMN', 3), ('WINTER', 4)]) 
 
 
# 通过__member__成员遍历枚举成员的名称 
for name, member in Season.__members__.items(): 
    print(name, member) 

输出结果:

SPRING Season.SPRING 
SUMMER Season.SUMMER 
AUTUMN Season.AUTUMN 
WINTER Season.WINTER 

enum.Flag类型

enum.Flag是用于创建二进制位形式枚举。

 
from enum import Flag, auto 
 
 
# 继承自Flag 
class Perm(Flag): 
    EXECUTE = auto() 
    WRITE = auto() 
    READ = auto() 
 
 
# 打印枚举成员 
print(list(Perm)) 
 
 
# 通过或运算组合功能,实现授权 
rw = Perm.READ | Perm.WRITE 
print(rw) 
 
# 提供与运算判断是否有该枚举成员,实现鉴权 
if rw & Perm.READ: 
    print("Allowed to read.") 
else: 
    print("Not allowed to read.") 
 
if rw & Perm.EXECUTE : 
    print("Allowed to execute.") 
else: 
    print("Not allowed to execute.") 

输出结果:

[<Perm.EXECUTE: 1>, <Perm.WRITE: 2>, <Perm.READ: 4>] 
Perm.READ|WRITE 
Allowed to read. 
Not allowed to execute. 

本文参考链接:https://blog.csdn.net/neweastsun/article/details/125117021