# 导入包 from enum import IntEnum # 定义枚举 class Season(IntEnum): Spring = 1 Summer = 2 Autumn = 3 Winter = 4 # 枚举类型转字符串 Season.Spring.name # 枚举类型转数字 Season.Spring.value # 字符串转枚举 Season['Spring'] # 数字转枚举 Season(1) # 遍历枚举成员 for elem in Season: elem.name elem.value elem # 比较大小 Season.Summer > Season.Spring