Predict the Enum Output
Intermediate · Predict the output · from Python Enums: Type-Safe Constants for Cleaner Code
From the lesson: Python Enums: Type-Safe Constants for Cleaner Code
Imagine you're building a card game. You need to represent the four suits: hearts, diamonds, clubs, and spades. You could use plain strings like "hearts", but what happens when someone types "Hearts" or "hart"? Your code breaks silently.
Read the lessonWhat will this code print? Think carefully about how enum comparison works and what auto() assigns.
from enum import Enum, auto
class Season(Enum):
SPRING = auto()
SUMMER = auto()
FALL = auto()
WINTER = auto()
print(Season.SPRING.value)
print(Season.WINTER.value)
print(Season.SUMMER == Season.SUMMER)
print(Season.SUMMER == 2)
More Enum exercises
- Create a Traffic Light EnumIntermediate
- HTTP Status Enum with MethodsIntermediate
- Fix the Enum BugIntermediate
- Build a Permission SystemAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz