Python 3.11+enumterm
enum.StrEnum
enum kinds
An enumeration whose members are also str (3.11+).
Syntax
class C(enum.StrEnum): ...
The trap shows up when you swap a hand-rolled class C(str, Enum) for StrEnum: since 3.11 the plain mixin renders as 'C.S' in str() and f-strings, while StrEnum renders as 'small'. Also non-obvious: auto() yields the member name lowercased rather than a number, and every value must be a string — a bare int in the class body raises.
Examples
import enum
class Size(enum.StrEnum):
S = 'small'
L = 'large'
print(Size.S == 'small') # → True
print(Size.S.upper()) # → SMALL
See also
Open in the full glossarydocs.python.orgReport an inaccuracy