The Enum Class
Define named constant groups.
The Enum Class is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Enums?
An enum (enumeration) is a set of named constant values that belong together. Instead of scattering magic strings or numbers through your code, you give each value a clear name.
- Self-documenting code.
- Protection against typos.
- A fixed, known set of choices.
Defining an Enum
Subclass Enum and list members as NAME = value. Conventionally member names are uppercase.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED)name and value
Every enum member has a .name (the identifier) and a .value (the assigned value).
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
c = Color.GREEN
print(c.name)
print(c.value)Members Are Singletons
Each member is a unique singleton object. Compare members with is or ==; the same member is always the exact same object.
from enum import Enum
class Color(Enum):
RED = 1
BLUE = 2
print(Color.RED is Color.RED)
print(Color.RED == Color.BLUE)Lookup by Value
Call the enum class with a value to get the matching member. An unknown value raises ValueError.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
print(Color(2))
try:
Color(99)
except ValueError as e:
print('Bad value:', e)Lookup by Name
Use bracket access with the member name string to look up a member by name. This is handy when reading names from config or user input.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
print(Color['GREEN'])Iterating Members
An enum is iterable. Looping yields each member in definition order, which is great for building menus or validating choices.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for c in Color:
print(c.name, c.value)String Values
Enum values are not limited to integers. Strings are common, especially for values that get serialized or stored.
from enum import Enum
class Status(Enum):
ACTIVE = 'active'
PAUSED = 'paused'
CLOSED = 'closed'
print(Status.PAUSED.value)
print(Status('closed'))auto() for Values
When the actual numbers do not matter, use auto() to assign values automatically, starting at 1 and counting up.
from enum import Enum, auto
class Direction(Enum):
NORTH = auto()
SOUTH = auto()
EAST = auto()
WEST = auto()
for d in Direction:
print(d.name, d.value)Enums in Functions
Using enums as function parameters makes intent clear and guards against invalid input. Compare members directly inside the logic.
from enum import Enum
class Light(Enum):
RED = 1
YELLOW = 2
GREEN = 3
def can_go(light):
return light is Light.GREEN
print(can_go(Light.GREEN))
print(can_go(Light.RED))Members in a Dictionary
Enum members are hashable, so they work as dictionary keys. This is a clean way to attach data to each named constant.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
hex_codes = {Color.RED: '#ff0000', Color.GREEN: '#00ff00'}
print(hex_codes[Color.RED])Quick Check
How do you correctly compare two enum members for identity?
Recap
You learned to define named constant groups with Enum.
- Members have
.nameand.value. - Look up by value with
Color(v), by name withColor['NAME']. - Enums are iterable and members are singletons.
- Use
auto()when exact values do not matter.
Frequently asked questions
Is the “The Enum Class” lesson free?
Yes — the full text of “The Enum Class” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Enum Class”?
Define named constant groups. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Enum Class” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Enum Class
- IntEnum and Flag
- NamedTuple Basics
- typing.NamedTuple