Python 3.4+enumterm
enum.Enum
basics
The base class for named enumerations. Every member is a unique symbolic object.
Syntax
from enum import Enum
class Color(Enum):
RED = 1
Members compare by identity, so Color.RED == 1 is False even when the member's value is that number; subclass IntEnum or StrEnum if you need comparison with raw values. Two members sharing a value are not an error — the second becomes an alias, hidden from iteration but still reachable by name. Iteration follows definition order, not value order.
Examples
from enum import Enum
class Color(Enum):
RED = 1; GREEN = 2; BLUE = 3
Color.RED # → <Color.RED: 1>
Color(2) # → <Color.GREEN: 2>
Color['BLUE'] # → <Color.BLUE: 3>
type(Color.RED) # → <enum 'Color'>
Color.RED == Color.RED # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy