Enums in Python 3: A Complete Guide

How-to basics · Updated November 23, 2026

Enums in Python 3 are classes that define a set of named constant values, created by subclassing enum.Enum. They give you readable, self-documenting code with built-in iteration, comparison, and safety against typos.

Why Enums Exist

Before enum was added in Python 3.4, developers used string literals, integer constants, or dictionaries to represent fixed choices. That works, but it has problems: a typo like "redd" silently becomes a new value, and nothing stops you from passing an invalid option to a function. Enums solve this by creating a closed set of members — you can only use the names you defined.

For example, a traffic light state as plain strings:

  • state = "green" — fine, but state = "greeen" is also accepted.
  • With an Enum, state = Light.GREEN is the only valid way, and Light.GREEN is a single object, not a string.

Defining an Enum

You create an Enum by subclassing Enum and assigning class attributes. Each attribute becomes a member with a name and a value. Here is a minimal example:

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

Now Color.RED is an Enum member. Its .name is "RED" and its .value is 1. You can iterate over the class: list(Color) returns [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>].

Values do not have to be integers — they can be strings, tuples, or any hashable object. Common practice uses integers for database IDs or strings for display labels.

Accessing Members and Values

You can access a member by name or by value. By name, use attribute access: Color.RED. By value, use the call syntax: Color(1) returns <Color.RED: 1>. If you try Color(5), you get a ValueError because 5 is not defined.

To get the name or value from a member:

  • Color.RED.name → 'RED'
  • Color.RED.value → 1

You can also look up by name using Color['RED'], which is useful when names come from user input or a database.

Auto-Numbering and Aliases

Writing values manually is fine, but Python offers auto() to assign sequential integers automatically:

from enum import Enum, auto

class Status(Enum):
    PENDING = auto()
    ACTIVE = auto()
    DONE = auto()

Status.PENDING.value is 1, ACTIVE is 2, and so on. If you need a different start, you can override _generate_next_value_.

Aliases occur when two names share the same value. For example:

class Color(Enum):
    RED = 1
    CRIMSON = 1

Here Color.CRIMSON is an alias for Color.RED. Iterating over the class yields only RED (the first defined), not CRIMSON. To keep aliases in iteration, use enum.unique decorator to forbid them entirely — it raises ValueError if duplicates exist.

Functional API and IntEnum

For quick one-off enums, you can use the functional API:

Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])

This creates the same enum as the class syntax. The second argument can be a list of names, a space-separated string, or a mapping of names to values.

If you need the enum members to behave like integers (e.g., for database comparisons), use IntEnum:

from enum import IntEnum

class Priority(IntEnum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3

Now Priority.HIGH == 3 is True and you can use it in arithmetic or as a list index. But be careful: IntEnum members compare equal to integers, which can hide bugs if you mix types.

Common Pitfalls and Best Practices

  • Don’t compare enums to strings or ints unless you use IntEnum. Color.RED == 1 is False for a plain Enum.
  • Use unique decorator to catch duplicate values early.
  • Don’t add methods that mutate state — enums are meant to be immutable. You can add methods, but they should not change the member’s value.
  • Be careful with pickling — enums are picklable by name, but if you define them inside a function, pickling may fail. Define them at module level.

Enums vs. Alternatives

When should you use an Enum versus a plain constant or a dataclass?

  • Plain constants (RED = 1) are fine for small, internal use, but they lack type safety and iteration.
  • String constants are readable but typo-prone.
  • Enums are best when you have a fixed set of related values that appear in multiple places — they give you a single source of truth and IDE autocomplete.
  • Dataclasses are for structured data with multiple fields, not for a set of named constants.

For example, if you have a function that accepts a color, using Color as the type hint makes the intent clear: def paint(c: Color) -> None:. A caller can only pass a valid member, and static type checkers (like mypy) will catch mistakes.

Frequently Asked Questions

Can I add methods to an Enum?

Yes. You can define methods inside the Enum class, just like any other class. For example, you could add a description() method that returns a human-readable string. However, you cannot change the member’s value after creation; enums are immutable.

How do I serialize an Enum to JSON?

By default, json.dumps cannot serialize an Enum member directly. You need to convert it. The simplest way is to use the .value attribute: json.dumps(Color.RED.value). For more complex cases, write a custom encoder that maps members to their names or values.

What is the difference between Enum and IntEnum?

An IntEnum member is also an int, so it can be compared to integers and used in arithmetic. A plain Enum member is not an integer — it is its own type. Use IntEnum when you need compatibility with integer-based APIs (like database fields) but want the readability of enums.

Can I use enums as dictionary keys?

Yes, enums are hashable, so they work as dictionary keys and set elements. This is a common pattern for mapping enums to configuration data.

How do I get a list of all enum values?

Use a list comprehension: [member.value for member in Color]. Or, if you want the members themselves, just call list(Color).

Are enums supported in older Python versions?

The enum module was added in Python 3.4. If you are using Python 2.7 or 3.3, you can install the enum34 backport package. However, for modern development, use Python 3.6 or later, where enums are fully stable and support features like auto().

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.