Sub topic 3
Iterating over members and understanding aliases
This chapter assumes you can define an Enum class and read .name and .value, as covered earlier. You should also know how == differs from is. Here you will loop over an enum, see what iteration leaves out, and learn how alias names work.
Iterating over an enum class
An enum class is iterable. Looping over the class itself yields its members in the order you defined them.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color.name, color.value)
print(len(Color))
print(list(Color))
print(Color.GREEN in Color)This prints:
RED 1
GREEN 2
BLUE 3
3
[<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>]
TrueThe class supports len(), in, and reversed(). It does not support indexing by position, so Color[0] fails. Square brackets look up a member by name, as in Color["RED"].
What an alias is
Two names can share one value. The first name you define becomes the real member. Every later name with the same value becomes an alias, which is just another name for that same member object.
from enum import Enum
class Status(Enum):
ACTIVE = 1
ENABLED = 1 # alias of ACTIVE
DISABLED = 2
print(Status.ENABLED is Status.ACTIVE) # True
print(Status.ENABLED.name) # ACTIVE
print(Status(1)) # Status.ACTIVE
print(len(Status)) # 2Status.ENABLED is not a second member. It is the same object as Status.ACTIVE, so its .name is ACTIVE. Looking up by value with Status(1) also returns the first definition.
Iteration skips aliases
Because aliases are not separate members, normal iteration never produces them.
from enum import Enum
class Status(Enum):
ACTIVE = 1
ENABLED = 1
DISABLED = 2
print(list(Status))
# [<Status.ACTIVE: 1>, <Status.DISABLED: 2>]A loop over Status runs twice, not three times. This matters when you build a menu or a validation list from an enum. The aliases will not appear, and that is usually what you want.
Seeing every name with `__members__`
The special attribute __members__ maps every name to its member, aliases included. It is a read-only mapping, so you cannot add or remove entries.
from enum import Enum
class Status(Enum):
ACTIVE = 1
ENABLED = 1
DISABLED = 2
print(list(Status.__members__))
# ['ACTIVE', 'ENABLED', 'DISABLED']
for name, member in Status.__members__.items():
print(name, "->", member)This prints:
ACTIVE -> Status.ACTIVE
ENABLED -> Status.ACTIVE
DISABLED -> Status.DISABLEDThe output makes the alias visible. The name ENABLED maps to Status.ACTIVE.
You can use this to list only the aliases. A name is an alias when it differs from the .name of the member it points to.
aliases = [
name
for name, member in Status.__members__.items()
if name != member.name
]
print(aliases) # ['ENABLED']| Tool | Includes aliases | Yields |
|---|---|---|
| `for m in Status` | No | Members |
| `list(Status)` | No | Members |
| `len(Status)` | No | A count of members |
| `Status.__members__` | Yes | Name-to-member pairs |
When aliases help and when they hurt
Aliases are useful for keeping an old name working after a rename. You can add the new name first and keep the old one as an alias. Existing code that uses the old name keeps working, and iteration shows only the new name.
They also hide mistakes. If you paste a value twice by accident, Python does not complain. It silently creates an alias, and a member you meant to add never exists. A later chapter shows how @unique turns this silent behavior into an error.
Exercise
Given the class below, what do len(Size), Size.BIG.name, and list(Size.__members__) return?
class Size(Enum):
LARGE = 3
BIG = 3
SMALL = 1
Show answer
len(Size) is 2, because BIG is an alias and iteration skips it. Size.BIG.name is 'LARGE', because LARGE was defined first and BIG is the same object. list(Size.__members__) is ['LARGE', 'BIG', 'SMALL'], because __members__ includes aliases.