Python Enums: Give Names to Magic Numbers

Python's Enum gives meaningful names to "magic numbers" or strings. Use it for fixed sets of options like statuses or categories to make code self-documenting. The footgun: don't compare members to raw values; compare member to member for type safety.
Why it exists
Code often uses "magic values"—raw numbers or strings like status = 1 or type = "admin"—to represent states. These are brittle, hard to remember, and prone to typos. Enums were created to solve this by providing a robust, readable, and type-safe way to define a fixed set of named constants.
The mental model
Think of an Enum as a formal contract for a variable's allowed values. It's a class that groups a set of symbolic names (members) bound to unique, constant values. Instead of passing around the number 2 to represent a "completed" state, you pass the object OrderStatus.COMPLETED. This makes the code's intent immediately clear to anyone reading it.
How it works
You create an Enum by subclassing enum.Enum and defining class attributes as your members. For example, class Color(Enum): RED = 1; GREEN = 2. Python then treats Color.RED as a special constant instance of the Color enum. You can access its name with .name (e.g., Color.RED.name is "RED") and its value with .value (e.g., Color.RED.value is 1). The enum.auto() helper can assign values for you automatically, typically starting from 1 and incrementing.
When to use it
Use Enums whenever you have a variable that can only take one of a few predefined values. This is common for things like status codes (e.g., PENDING, PROCESSING, FAILED), user roles (ADMIN, EDITOR, VIEWER), or configuration settings (DEVELOPMENT, STAGING, PRODUCTION). They integrate well with type hints and IDEs, providing autocompletion and static analysis checks.
When not to use it
Avoid Enums for values that are not a fixed, known set. If you need to represent a user's ID, a timestamp, or any other unbounded or dynamic data, an Enum is the wrong tool. They are for symbolic constants, not for storing arbitrary data. Also, while IntEnum or StrEnum can make members behave like their base types, this can reintroduce ambiguity if you're not careful.
One canonical example
A common use case is defining order statuses. You would define a class OrderStatus(Enum) with members like PENDING = auto(), SHIPPED = auto(), and DELIVERED = auto(). A function process_order would then take an argument of type OrderStatus. Inside the function, you would check the status with if status == OrderStatus.SHIPPED:, which is far more readable than if status == 2:. This ensures only valid statuses can be used and makes the logic clear.
Interview question
What primary problem do Python Enums address in code?
- a.Optimizing memory usage for large collections of data.
- b.Preventing runtime errors caused by incorrect data types.
- c.Making code more readable and less prone to errors when using fixed sets of values.Correct
- d.Enabling dynamic creation of new constant values during program execution.
Why? this is the answer
The card states Enums solve the problem of "magic values" by providing a "robust, readable, and type-safe way to define a fixed set of named constants," making code's intent "immediately clear." While Enums contribute to type safety, their primary purpose is to replace obscure "magic numbers" with meaningful names for fixed sets of choices, improving readability and reducing errors from typos or misunderstandings.
Just read this? Test yourself on what you have been reading.
Read the original → docs.python.org
- #python
- #enum
- #data structures
- #best practices
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles