Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python Enum aliases: iteration and member lookup have different domains

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

An Enum alias is a second declared name that refers to an existing member with the same value.

Download Python source kit

Operation contract

The receipt status definition keeps NEW as an alias of PENDING. Ordinary iteration returns each canonical member once, while __members__ contains every declared name, including NEW. Reading the alias name returns the canonical member whose name is PENDING. That matters when a received value must round-trip through a stored status: aliases preserve lookup compatibility but do not preserve the original spelling.

Failure and ownership boundary

Use unique when duplicate values are a schema mistake rather than an intended compatibility policy. Enum aliases are distinct from two external strings mapped to one field by a parser. Do not serialize the Python class name as an enduring wire identifier. Python Enum: parse wire values into a closed state set, Python JSON validation: reject duplicate members and non-integer amounts and Python dictionaries: insertion order and duplicate-key replacement remain separate concerns.

Working program

python
from enum import Enum, unique

class ReceiptStatus(Enum):
    PENDING = "pending"
    NEW = "pending"
    POSTED = "posted"

print("iteration:", [member.name for member in ReceiptStatus])
print("declared:", list(ReceiptStatus.__members__))
print("alias identity:", ReceiptStatus.NEW is ReceiptStatus.PENDING)
print("alias canonical name:", ReceiptStatus["NEW"].name)
try:
    unique(ReceiptStatus)
except ValueError:
    print("duplicate values rejected")

Output

Output
iteration: ['PENDING', 'POSTED']
declared: ['PENDING', 'NEW', 'POSTED']
alias identity: True
alias canonical name: PENDING
duplicate values rejected

Costs and limits

Lookup uses the declared member mapping. Iteration visits canonical members, so counts differ when aliases exist. Creating a large enum and its mappings has construction and retained-object costs; this two-value definition is not a benchmark.

Common Mistakes

  • Iteration omits aliases; use __members__ when examining declared names.
  • An alias cannot retain an independently chosen canonical name.

Connected lessons

Python Enum: parse wire values into a closed state set, Python JSON validation: reject duplicate members and non-integer amounts, Python hash and equality: immutable dictionary keys.

python
enum-alias-contract
Storage details