Python 3.4+enumterm
Enum iteration and comparison
basics
Enumerations support iteration through list(MyEnum) and comparison with is / ==. IntEnum also supports < and >.
Syntax
list(MyEnum) # все элементы
for m in MyEnum: ...
member is MyEnum.X
A plain Enum member is not equal to its own value: comparing it with a number or a string gives False, so use .value or switch to IntEnum/StrEnum. Iteration follows definition order, not value order, and skips aliases — look at __members__ to see those. Members are singletons, so is is enough for comparison and it stays correct even if two different enums end up sharing values.
Examples
from enum import Enum
class Day(Enum):
MON=1; TUE=2; WED=3
list(Day) # → [<Day.MON:1>, <Day.TUE:2>, <Day.WED:3>]
Day.MON is Day.MON # → True
Day.MON == Day.MON # → True
Day.MON == 1 # → False (Enum не совместим с int)
Day.MON in Day # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy