# 枚举类型不能做大小比较,但是可以做身份比较和等值比较。print(f'SeasonEnum.SPRING is SeasonEnum.SPRING: {SeasonEnum.SPRING is SeasonEnum.SPRING}')print(f'SeasonEnum.SPRING is SeasonEnum.WINTER: {SeasonEnum.SPRING is SeasonEnum.WINTER}')# 这是身份比较,每个对象都可以进行身份比较,枚举类也不例外。print(f'SeasonEnum.SPRING == SeasonEnum.SPRING: {SeasonEnum.SPRING == SeasonEnum.SPRING}')print(f'SeasonEnum.SPRING != SeasonEnum.WINTER: {SeasonEnum.SPRING != SeasonEnum.WINTER}')
输出
SeasonEnum.SPRING is SeasonEnum.SPRING: TrueSeasonEnum.SPRING is SeasonEnum.WINTER: FalseSeasonEnum.SPRING == SeasonEnum.SPRING: TrueSeasonEnum.SPRING != SeasonEnum.WINTER: True
遍历枚举类
代码
# 如果尝试遍历枚举类型,则后面重复的不会被打印出来。但是,如果想要获取别名,# 我们可以使用属性“members”,它是一个OrderedDict,包括所有定义的枚举名称,包括别名。for name, member in SeasonEnum.__members__.items():print(name, '=>', member)for name, member inenumerate(SeasonEnum):print(name, '=>', member)