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

Python · Module

enum

Python's enum module defines enumerations: named members associated with values. Enumerations support class or functional construction, iteration, lookup by value, and lookup by name.

Since
3.4

How it works

The enum module lets you define a type whose members have symbolic names and associated values. Members can be retrieved by calling the enumeration with a value, indexed by name, or iterated in definition order.

Syntax

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

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

Parameters

NameTypeDescription
definitionclass or Enum(name, members, *, module=None, qualname=None, type=None, start=1)Enumeration class declaration or functional-construction arguments that define member names and values.

Returns

An enumeration class with named members; calling that class with a member value returns the corresponding member, and indexing it by name returns the named member.

Key points

  • An enumeration groups named members with unique values.
  • Iterating over an enumeration yields canonical members in declaration order.
  • Use call syntax, such as Color(1), to retrieve the member whose value is 1.
  • Use index syntax, such as Color["RED"], to retrieve a member by its name.
  • The module supports both class-based and functional construction.
  • The page documents the enum module's API reference, including enumeration behavior and available definitions.

Common mistakes

  • Iteration omits aliases and yields canonical members in the order they were defined.
  • Calling an enumeration with an unknown value does not select a member; value-based lookup requires a matching value.
  • Name lookup uses indexing, so the key must match the member name rather than its value.

Example: Create and use an Enum

from enum import Enum

class Status(Enum):
    PENDING = "pending"
    DONE = "done"

status = Status("done")
assert status is Status.DONE
assert Status["PENDING"] is Status.PENDING
print([member.name for member in Status])

Defines a small enumeration, looks up a member by its value and name, then iterates over its canonical members.

When to use it

Model finite state values

Represent a field constrained to a fixed set of states.

Organize related constants

Replace scattered constants with a named, iterable group.

Perform value-based lookup

Convert external values into validated symbolic members.

Perform name-based lookup

Access members dynamically when the member name is supplied as text.

Interview questions

QWhat is an Enum and how are its members accessed?

Enum members are symbolic names bound to values, and each member belongs to the enumeration type. They can be accessed as attributes such as Status.DONE, looked up by value with Status(value), and looked up by name with Status["DONE"].

Follow-up: How does iteration treat aliases? It returns canonical, non-alias members in definition order.

QHow can an enumeration be created?

The class form defines an enumeration by declaring members in a subclass of Enum. The functional form calls Enum with the enumeration name and a source of member names or name-value pairs.

Follow-up: When would the functional form be useful? It is useful when the names or values are assembled dynamically rather than written as a class declaration.

QWhat operations does the enum documentation define for an enumeration?

Iteration produces the enumeration's canonical members in definition order. A member can also be selected by its value through call syntax or by its name through index syntax.

Follow-up: What distinction must callers preserve? A member name and its value are separate lookup keys, so name lookup and value lookup use different operations.

  • Enum
  • IntEnum
  • Flag
  • IntFlag
  • unique
  • auto

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