Skip to main content

enum

Symbolic constant collections. Enum members are singletons with name and value. Subclasses can mix in int, str, or other types so members behave like the underlying value.

Source-of-record: Lib/enum.py, enum docs.

Base classes

ClassMix-inUse
Enum(none)Plain enumeration.
IntEnumintMembers are also int.
StrEnumstrMembers are also str (3.11+).
Flag(none)Bit flags; supports |, &, ^, ~.
IntFlagintBit flags as int.
ReprEnum(none)Use the mixed-in type's repr (3.11+).

Member declaration

class Color(Enum):
RED = 1
GREEN = 2
BLUE = auto()

enum.auto() generates the next value using _generate_next_value_.

Decorators and helpers

HelperRole
@uniqueReject duplicate values.
@verify(...)Apply a verifier; see below.
@member / @nonmemberForce a class-body assignment into / out of the enum (3.12+).
@propertyClass-body properties are not turned into members.
auto()Auto-assign value.
Enum.__init_subclass__(cls, **kw)Hook for boundaries (3.12+).

Verifier flags

FlagEffect
UNIQUEAll members distinct.
CONTINUOUSNo gaps in IntEnum values.
NAMED_FLAGSAll flag bits are named.

Boundary

For Flag / IntFlag, members can be combined; out-of-range bits are handled per the boundary argument:

ValueBehaviour for unknown bits
STRICTRaise ValueError.
CONFORMStrip unknown bits.
EJECTFall back to a plain int.
KEEPKeep them in the flag value.

Functional API

Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])
Status = IntEnum('Status', 'OK FAIL', start=10)

Accepts a sequence of names, a name-value mapping, or a space/comma separated string.

Member introspection

Attribute / methodMeaning
member.name / member.valueSymbolic name / underlying value.
EnumClass.__members__Mapping of name -> member.
list(EnumClass)Members in definition order.
EnumClass(value)Lookup by value.
EnumClass['NAME']Lookup by name.

Gopy status

AreaState
Enum, IntEnum, StrEnum, ReprEnumComplete.
Flag, IntFlag (all boundaries)Complete.
auto, unique, verifyComplete.
@member / @nonmemberComplete.
Functional APIComplete.

Reference

  • CPython 3.14: enum.
  • Lib/enum.py.
  • module/enum/. gopy port.
  • PEP 435.