Enums (the enum module)

An enum is a class that bundles a fixed set of named constant values into one self-documenting type, so a variable can only ever hold one of the choices you defined.

Learn Enums (the enum module) in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

If you have ever passed status="acttive" with a typo and watched it silently break, enums are the fix. They turn loose magic values into named members the editor and interpreter can check.

Subclass Enum and list your members as NAME = value . Each member is a singleton object you compare by identity:

You can look a member up by value with Color(2) or by name with Color["GREEN"] :

Enums are iterable in definition order, which makes them perfect for building menus, validating input, or generating dropdowns. And when the exact numbers do not matter, auto() assigns them for you:

With auto() you only maintain the names. Add, remove, or reorder members without ever renumbering by hand.

An IntEnum member is an integer, so it sorts and compares like one. Use it for ordered levels such as priorities or log severities:

A Flag enum lets a single value hold a combination of members using the bitwise | operator — perfect for permissions or feature toggles:

Complete the enum so it auto-numbers its members and reads the right attributes. Replace each ___ and match the expected output.

The two prints use d.name (gives EAST ) and d.value (gives 2 ).

✅ Compare members to members: if status == Status.ACTIVE: (or use IntEnum if you need int comparisons).

✅ Give each member a unique value, or add @enum.unique above the class to raise an error on duplicates.

✅ Enums are final. Define every member inside the class body.

Model an order's lifecycle with an enum, then advance an order to the next state by looking up the following member by value.

Go deeper with the official Python documentation:

Lesson complete — your constants have names and types now!

You can define an Enum , read .name and .value , iterate over members, auto-number with auto() , compare ordered IntEnum values, and combine options with Flag .

🚀 Up next: namedtuple & NamedTuple — lightweight, immutable records with named fields.

Practice quiz

For a plain Enum Color with RED = 1, what is Color.RED == 1?

  • True
  • It raises an error
  • False
  • 1

Answer: False. A plain Enum member is not equal to its raw value, so Color.RED == 1 is False — protecting against stray raw values.

What is the difference between a member's .name and .value?

  • .name is the identifier you typed; .value is the data you assigned
  • .name is the value, .value is the name
  • They are always equal
  • .value only exists on IntEnum

Answer: .name is the identifier you typed; .value is the data you assigned. Color.RED.name is the string 'RED' (the identifier); Color.RED.value is the assigned data such as 1.

How do you look up an enum member by its value, e.g. value 2 of Color?

  • Color.value(2)
  • 2

Calling the enum like Color(2) performs a value lookup and returns Color.GREEN.

How do you look up an enum member by its name, e.g. 'BLUE'?

  • Color('BLUE')

Subscript syntax Color["BLUE"] looks a member up by name and returns Color.BLUE.

What value does the first auto() member receive by default?

  • 1
  • 0
  • None
  • A random integer

Answer: 1. auto() produces consecutive integers starting at 1 by default, so the first member is 1.

Why might you choose IntEnum over a plain Enum?

  • It uses less memory
  • It auto-numbers members
  • Its members behave like integers, so they compare and sort with < and >
  • It allows duplicate values

Answer: Its members behave like integers, so they compare and sort with < and >. IntEnum members ARE integers, so Priority.HIGH > Priority.LOW works and they interoperate with code expecting ints.

What does a Flag enum let you do with the | operator?

  • Concatenate names
  • Combine several members into one value, like Perm.READ | Perm.WRITE
  • Compare values numerically
  • Sort the members

Answer: Combine several members into one value, like Perm.READ | Perm.WRITE. Flag enums support bitwise | to combine members, ideal for permissions and feature toggles.

In a plain Enum, what happens if two members are assigned the same value?

  • A SyntaxError at import
  • Both are silently dropped
  • The values are auto-incremented
  • The second becomes an alias of the first, not a new member

Answer: The second becomes an alias of the first, not a new member. Duplicate values create an alias: the second name refers to the first member unless @enum.unique forbids it.

Can you add a new member after the enum class is defined, e.g. Color.YELLOW = 4?

  • Yes, anytime
  • No, enums are final and this raises an error
  • Only inside __init__
  • Only for IntEnum

Answer: No, enums are final and this raises an error. Enums are final; every member must be defined in the class body, and reassigning/extending raises an error.

What does len(MyEnum) return?

  • Always 0
  • The largest member value
  • The number of members defined in the enum
  • The length of the longest member name

Answer: The number of members defined in the enum. Enums are iterable and len() counts their members; a three-member enum returns 3.