Python Enum Types — ELI5

Imagine a traffic light. It has exactly three states: red, yellow, and green. You’d never expect it to turn purple or plaid. The choices are fixed.

In code, developers often use numbers or short strings to represent these kinds of fixed choices. Maybe 1 means “pending,” 2 means “approved,” and 3 means “rejected.” But a month later, nobody remembers what 2 means. Was it approved or denied? Is 4 even valid?

Enum (short for “enumeration”) solves this by giving those fixed choices proper names. Instead of status = 2, you write status = Status.APPROVED. Now every developer — including future-you — instantly knows what it means.

Think of it like sports jerseys. The number 7 on a jersey could mean anything. But “Ronaldo” means exactly one player. Enums are the names on the back of the jersey.

The other great thing: you can’t accidentally create a new choice. If someone tries to use Status.MAYBE, Python immediately complains. With plain numbers, someone could quietly use status = 99 and nobody would catch it until something broke.

Enums keep your code honest. The choices are defined once, named clearly, and enforced by Python itself. No guessing, no magic numbers, no “wait, what does 3 mean again?”

The one thing to remember: Enums replace mystery numbers and strings with named, fixed choices that Python enforces — like putting names on jerseys so nobody argues about who’s who.

pythonstandard-librarytypes

See Also

  • Python Atexit How Python's atexit module lets your program clean up after itself right before it shuts down.
  • Python Bisect Sorted Lists How Python's bisect module finds things in sorted lists the way you'd find a word in a dictionary — by jumping to the middle.
  • Python Contextlib How Python's contextlib module makes the 'with' statement work for anything, not just files.
  • Python Copy Module Why copying data in Python isn't as simple as it sounds, and how the copy module prevents sneaky bugs.
  • Python Dataclass Field Metadata How Python dataclass fields can carry hidden notes — like sticky notes on a filing cabinet that tools read automatically.