Python
Structural Pattern Matching
23 flashcards · answers and spaced-repetition review in the KnowCard app
What does this print?
```
d = {"a": 1, "b": 2, "c": 3}
match d:
case {"a": 1}:
print("match")
case _:
print("no")
```
What does this print?
```
match 5:
case 1 | 2 | 3:
print("low")
case 4 | 5 | 6:
print("mid")
```
What does this print?
```
match [1, 2]:
case (1, 2):
print("tuple pat")
case [1, 2]:
print("list pat")
```
What happens when you run this?
```
x = 0
match "hello":
case x:
print("matched")
case "hello":
print("literal")
```
You have a module constant MAX = 100. Why does this always match, printing "hit" even when n is 5?
```
match n:
case MAX:
print("hit")
```
What does this print?
```
from enum import Enum
class C(Enum):
RED = 1
BLUE = 2
match C.BLUE:
case C.RED:
print("red")
case C.BLUE:
print("blue")
```
What does this print?
```
cmd = "start"
match cmd:
case "stop":
print("stopping")
case "pause":
print("pausing")
print("done")
```
What is the difference in side effects between case _: and case other: when both are used as the catch-all final case?
What does this print?
```
def f(x):
match x:
case []:
return "empty"
case [a]:
return f"one:{a}"
case [a, b, *rest]:
return f"{a},{b}+{len(rest)}"
print(f([10, 20, 30, 40]))
```
Why does Python reject case [x]: when you mean to match a one-element sequence, in the context where you wrote a tuple pattern (x) — and what's the fix?
What does this print?
```
d = {"name": "Sam"}
match d:
case {"name": str() as n, "age": _}:
print(f"full {n}")
case {"name": str() as n}:
print(f"name {n}")
```
What does this print?
```
class Point:
__match_args__ = ("x", "y")
def __init__(self, x, y):
self.x, self.y = x, y
match Point(0, 7):
case Point(0, 0):
print("origin")
case Point(0, y):
print(f"y-axis {y}")
```
Given a complex number z = 4+0j, what does this print?
```
match z:
case complex(real=r, imag=0):
print(f"real {r}")
case complex():
print("complex")
```
What does this print?
```
for v in (5, -3, 0):
match v:
case n if n > 0:
print("pos")
case n if n < 0:
print("neg")
case _:
print("zero")
```
What does this print?
```
match 10:
case int() | float():
print("number")
case 10:
print("ten")
```
What does this print?
```
_ = "keep"
match "x":
case _:
print(_)
```
Why does this raise an error?
```
import math
match x:
case math.inf | -math.inf:
...
```
What does this print?
```
match ["go", 5]:
case [str(cmd), int(n)] if n > 0:
print(f"{cmd}x{n}")
case [str(cmd), _]:
print(cmd)
```
In a class/type pattern, what does case int(x): do — and how does it relate to case int() as x:?
Class A has __match_args__ = ("x", "y", "z"). Why is case A(1, y=2, 3): invalid, and what's the correct ordering?
This OR pattern is rejected. What rule does it break, and how do you fix it?
match v:
case int() as n | float():
print(n)
Before Python 3.10, dispatching on one value against many cases meant a long if-elif-else chain like if cmd == "a": ... elif cmd == "b": .... What feature replaced this, and what repetition does it remove?
Which of these are valid literal patterns, and which is not?
case -5:
case None:
case True:
case -math.inf:
Start learning today
Free to start — download the app or use it in your browser.
