Part 4 · Section 10

Enumerations

Plenty of values in a program come from a small, fixed set: the status of a job, the days of the week, the HTTP status codes a server can send back. You could represent them with bare strings or integers, but then nothing stops a typo like 'pnding' from slipping through, and nothing tells a reader which values are allowed. An enumeration turns such a set into a class whose instances are exactly the allowed values. Each one has a name, a value and an identity, and it can be looked up, iterated over and compared safely.

Enumerations look like ordinary classes but follow special rules, because a metaclass (EnumType) builds them. Those rules are the subject of this section. The enum module also changed a lot between Python 3.11 and 3.13, so the modern behavior is pointed out as we go.

In this section you’ll learn:

Lectures

  1. 01Enumerations✓
  2. 02Aliases✓
  3. 03Customizing and Extending Enumerations✓
  4. 04Automatic Values✓

Practice: quiz & exercises

12 quiz questions · 4 coding exercises

Go to practice →