Modern PythonLesson 3 of 3
Pattern Matching & Enums
Python 3.10 introduced match/case. It looks like switch/case but is far more powerful: it can destructure sequences, match dictionary shapes, check types and attributes of objects, and add guard conditions, all in one construct.
There is no fall-through. Each case is independent, as if every case ended with break. The wildcard pattern _ plays the role of default. You can combine alternatives with |, as in case "y" | "yes":.
The biggest trap: a bare name in a pattern is a capture, not a comparison. case RED: does not compare against a variable called RED; it matches anything and assigns it to RED. To compare against a constant, use a dotted name such as Color.RED or a literal.
Python also has real Enums, unlike JS where "enums" are usually frozen objects (TypeScript's enum is a compile-time feature). Python Enums are classes: members have a name and a value, the enum is iterable, members are singletons you can compare with is, and typos raise AttributeError instead of silently producing undefined. StrEnum (3.11+) gives you members that are also real strings, which is handy for JSON and APIs.
Quick reference
| JavaScript | Python | Note |
|---|---|---|
| switch (x) { case 1: ... break } | match x: case 1: | No break, no fall-through |
| default: | case _: | Wildcard pattern |
| case 'y': case 'yes': | case "y" | "yes": | Or-patterns |
| No destructuring in switch | case [cmd, arg]: | Sequence patterns |
| if (e.type === 'click') | case {"type": "click", "x": x}: | Mapping patterns |
| No guard clause | case [x, y] if x > y: | Guards |
| Object.freeze({ A: 'a' }) | class E(Enum): A = 'a' | Real enum type |
Side by side
// JavaScript
switch (status) {
case "active": handleActive(); break;
case "inactive": handleInactive(); break;
default: handleUnknown();
}
const Status = Object.freeze({
ACTIVE: "active",
INACTIVE: "inactive",
});# Python match/case (3.10+)
match status:
case "active":
handle_active()
case "inactive" | "disabled":
handle_inactive()
case _:
handle_unknown()
# Sequence patterns
match command.split():
case ["go", direction]:
move(direction)
case ["pick", "up", item]:
pickup(item)
# Mapping patterns + guards
match event:
case {"type": "click", "x": x, "y": y}:
handle_click(x, y)
case {"type": "key", "key": k} if k in VALID:
handle_key(k)
# Enums
from enum import Enum, StrEnum, auto
class Color(Enum):
RED = auto()
GREEN = auto()
class Status(StrEnum):
ACTIVE = "active"
for c in Color:
print(c) # Color.RED, Color.GREENExercise
Real Python, running in your browserWrite using match/case and Enum:
Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment