Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

Enum

Enum defines a fixed set of symbolic names associated with values. Its members support identity comparison, iteration, and lookup by value or name.

Since
3.4

How it works

Subclass Enum to create a class whose named members represent a fixed set of values. Members are singleton objects: compare them by identity, obtain one from its value by calling the enumeration, and access its declared name or value through name and value.

Syntax

class enum.Enum(*enums)

Key points

  • Enum groups symbolic member names with their associated values.
  • Calling the enumeration with a value performs value-based member lookup.
  • Square brackets perform name-based lookup, such as Color['RED'].
  • A member's name is available through its name attribute.
  • A member's associated value is available through its value attribute.
  • Choose readable class declaration syntax or the functional constructor when creating an Enum dynamically.

Common mistakes

  • An Enum class is specialized for defining members, so do not treat it like an ordinary class whose instances are freely created.
  • Iteration follows declaration order and excludes aliases.

Example: Define and use an enumeration

from enum import Enum

class Color(Enum):
    RED = "red"
    GREEN = "green"

print(Color.RED.name)
print(Color("green") is Color.GREEN)
print([color.value for color in Color])

This defines an enumeration, accesses a member name and value, resolves a member from its value, and iterates over the members.

When to use it

Fixed domain choices

Represent a finite set of states, modes, or categories without scattering string or integer constants through application code.

Reliable state comparisons

Use member identity to make state comparisons explicit and type-safe.

Input and configuration decoding

Convert serialized values back into strongly named members with Enum(value).

Interview questions

QWhat is Python's Enum class used for?

Enum is the base class for creating enumerated constants. Define a subclass and assign names to members, such as RED = 1 and BLUE = 2.

Follow-up: How can a member be retrieved from its value? Call the enumeration class with that value, for example Color(1). If no member has that value, the lookup raises ValueError.

QHow do Enum members behave and how are they inspected?

Members are compared by identity, so Color.RED is the appropriate comparison. Each member exposes name, the defining name, and value, the assigned value.

Follow-up: What does iteration return when aliases exist? It returns only canonical members, not aliases, while preserving definition order.

QWhat are the two documented ways to create an enumeration?

An enumeration can be created with class syntax or with functional syntax such as Enum('Color', [('RED', 1), ('BLUE', 2)]). The functional form returns a new Enum class.

Follow-up: What other lookup syntax is documented? Index syntax retrieves a member by name, for example Color['RED']; an invalid name raises KeyError.

  • EnumType
  • IntEnum
  • Flag
  • IntFlag
  • unique()

Source: enum — Support for enumerations. Checked 2026-10-15.