Sub topic 5
Letting Python choose values with auto()
This chapter assumes you can define an Enum class and read a member's .name and .value. It also assumes you know what @unique does. Often the specific values don't matter and you only need each member to be distinct. In that case auto() lets Python pick them.
The basic behavior
auto() is a placeholder that you assign in the class body. When the class is created, Python replaces each placeholder with a real value.
from enum import Enum, auto
class Direction(Enum):
NORTH = auto()
EAST = auto()
SOUTH = auto()
WEST = auto()
print(list(Direction))
print(Direction.SOUTH.value)Output:
[<Direction.NORTH: 1>, <Direction.EAST: 2>, <Direction.SOUTH: 3>, <Direction.WEST: 4>]
3For a plain Enum, the values are integers that start at 1 and go up by one. You can no longer introduce an alias by accident with a copy-pasted number, because no numbers are written.
Mixing auto() with explicit values
You can combine auto() with values you write yourself. Each auto() continues from the value before it.
from enum import Enum, auto
class Http(Enum):
OK = 200
CREATED = auto()
ACCEPTED = auto()
print(Http.CREATED.value, Http.ACCEPTED.value) # 201 202If you put an explicit value in the middle of the class, later auto() calls continue from it. The result depends on declaration order, so keep explicit values at the top or avoid mixing the two styles.
Different base classes, different values
The value auto() produces depends on the enum type. This is why auto() is more useful than typing numbers by hand.
| Base class | What `auto()` produces |
|---|---|
| `Enum`, `IntEnum` | 1, 2, 3, ... |
| `Flag`, `IntFlag` | 1, 2, 4, 8, ... (powers of two) |
| `StrEnum` | The member name in lowercase |
from enum import Flag, StrEnum, auto # StrEnum requires Python 3.11+
class Perm(Flag):
READ = auto()
WRITE = auto()
EXECUTE = auto()
class Mode(StrEnum):
READ = auto()
WRITE = auto()
print(Perm.READ.value, Perm.WRITE.value, Perm.EXECUTE.value) # 1 2 4
print(Mode.READ.value) # readPowers of two are what make Flag members combinable with | without overlapping bits. Writing them by hand is easy to get wrong, for example by typing 3 where you meant 4.
Working with @unique
auto() never creates an alias on its own, because each call yields a new value. Aliases can still appear if you mix auto() with explicit values that collide:
from enum import Enum, auto, unique
@unique
class Level(Enum):
LOW = 1
MEDIUM = auto() # 2
HIGH = 2 # same value as MEDIUMDefining this class raises ValueError. Without @unique, HIGH would silently become an alias of MEDIUM. Using @unique together with auto() makes that kind of collision visible immediately.
When not to use auto()
The numbers auto() picks depend on member order. That has consequences:
- Reordering or inserting members changes the values of the members after them.
- If you store values in a database, a file, or an API response, those stored values will then point at the wrong members.
- For persisted or exchanged data, write the values explicitly. The same applies when the value has meaning, as
200does in the HTTP example.
Use auto() when the value is only an internal label and members are compared by identity, such as the Direction example above.
Customizing the generated values
auto() calls a hook named _generate_next_value_ to compute each value. You can override it in your own enum to change the rule. A common choice is to make each value equal to its name:
from enum import Enum, auto
class Token(Enum):
@staticmethod
def _generate_next_value_(name, start, count, last_values):
return name
IF = auto()
ELSE = auto()
print(Token.IF.value) # IFThe hook receives the member name, the starting number, how many members exist so far, and the list of earlier values. In older Python versions, define the hook before any members, as shown here. Later chapters return to this hook.
Exercise
What are the values of A, B and C in this plain Enum, and why? class T(Enum): A = auto(); B = 5; C = auto()
Show answer
A is 1, B is 5 and C is 6. The first auto() starts at 1. The explicit 5 is written as is. The next auto() continues from the previous value, so it yields 6.