python - 如何在 python Enum 中更改 auto 的基值?

标签 python python-3.x enums

假设我有以下代码,如何更改 auto 的基值,使 Animal.ant 成为任意值,例如10 个,而不是 1 个?

from enum import Enum, auto

class Animal(Enum):
    ant = auto()
    bee = auto()
    cat = auto()
    dog = auto()

最佳答案

如果您希望某些成员具有某些值,只需分配它们即可:

class Animal(Enum):
    ant = 10
    bee = auto()
    cat = auto()
    dog = auto()

这将自动调整后续成员的值:

>>> list(Animal)
[<Animal.ant: 10>, <Animal.bee: 11>, <Animal.cat: 12>, <Animal.dog: 13>]

关于python - 如何在 python Enum 中更改 auto 的基值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943172/

相关文章:

python - 形状绘图出错

Python:使用 Scapy 发送 IGMP 数据包

python - 使用 execfile() 设置函数变量

python - 正则表达式 match() 无法捕获 python 中的简单模式

python - 在 Python 3 中将字符串转换为 int 或 float?

enums - 与语言中的普通枚举相比,built_value 枚举有什么好处

jsf - 在 p :dataTable filters 中显式使用隐式 JSF 转换器 javax.faces.convert.EnumConverter

python - PIL 安装失败缺少 :stdarg. h

python - Python 3.x 和 Python 2.7 中 dict.values() 和 dict.keys() 相等之间的行为不一致

java - Java 枚举中的静态(和最终)字段初始化