Capture Names and Use Wildcards
Core idea
A bare name in a pattern, such as case label:, captures the subject into that name. It does not compare the subject with the previous value of label. A wildcard _ matches without binding. A dotted name such as Status.READY is a value pattern and compares against the value found there. An unguarded capture or wildcard must be the last case.
label = "old"match "new": case label: print(label)print(label)The capture rebinds label; case does not create a new scope.
Ignore the subject
The wildcard leaves the existing underscore name alone.
Compare a named constant
class Status: READY = "ready"match "wait": case Status.READY: print("start") case _: print("hold")The dotted pattern compares values; it does not bind READY.
Common mistake
Do not put case READY: before other cases when READY is a bare name. It captures every subject and makes later cases unreachable, causing SyntaxError. Use a literal or dotted value instead.
Practice
Change the dotted pattern to a bare capture in a single-case match. Observe the binding, then restore the comparison.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distinguish captures and value patterns
Check your understanding of capture and wildcard.
What is printed?
n = 2match 8: case n: passprint(n)What is printed?
_ = "saved"match 5: case _: passprint(_)What is printed?
class State: READY = "ready"match "idle": case State.READY: print("ready") case _: print("wait")Which pattern compares with a named constant instead of capturing?