Sub topic 3
Capturing values and the _ wildcard
This chapter assumes you can write a basic match statement with literal case blocks, as in the earlier chapters. Here you learn the two patterns that match anything: the capture pattern and the wildcard.
Capture patterns
A bare name in a case line is a capture pattern. It always matches, and it binds the subject to that name.
def describe(value):
match value:
case 0:
return "zero"
case n:
return f"number {n}"
print(describe(0)) # zero
print(describe(7)) # number 7When value is 7, the literal 0 fails. The pattern n then succeeds and sets n = 7 before the case body runs.
The wildcard
A single underscore, _, also matches anything. Unlike a capture, it binds nothing. Use it when you need a default branch and don't need the value.
def status(code):
match code:
case 200:
return "OK"
case 404:
return "Not Found"
case _:
return "Other"
print(status(404)) # Not Found
print(status(503)) # OtherWriting case other: would also work as a default. Choose _ when you won't use the value. It tells the reader that nothing is captured.
Captures inside larger patterns
Captures and wildcards also work as parts of bigger patterns. A later chapter covers sequence patterns in detail. This preview shows the idea:
def second(pair):
match pair:
case (_, y):
return y
case _:
return None
print(second((1, 2))) # 2
print(second(5)) # NoneThe pattern (_, y) requires a two-element sequence. It ignores the first element and binds the second to y. You can use _ several times in one pattern, because it never binds. You cannot reuse a real name, so case (x, x) is a syntax error.
Captures are ordinary variables
A capture assigns to a normal local variable. The name is not limited to the case block, and it stays set after the match ends.
match 42:
case n:
pass
print(n) # 42This also means a capture can overwrite a variable that already has that name. The next section explains why that matters.
Irrefutable patterns must come last
A pattern that cannot fail is called irrefutable. Both _ and a bare capture name are irrefutable. Python checks that nothing follows one. If you put case _: or case n: before other cases, you get a SyntaxError saying the remaining patterns are unreachable. Always put these patterns at the end.
Summary of the two patterns
| Pattern | Matches | Binds a name | Typical use |
|---|---|---|---|
| `name` | Anything | Yes | Default branch that uses the value |
| `_` | Anything | No | Default branch, or skipping a part |
Exercise
What does this code print, and why? def f(x): match x: case 1: return "one" case y: return f"got {y}" print(f(1), f(9))
Show answer
It prints "one got 9". For 1, the literal pattern matches first and returns "one". For 9, the literal fails, and the capture pattern y matches anything, so it binds y to 9 and returns "got 9".
Exercise
Why does putting case _: before case 200: in a match statement fail, and how do you fix it?
Show answer
The wildcard is irrefutable, so it would catch every subject and leave case 200: unreachable. Python reports this as a SyntaxError. Move case _: to the last position.