# Simulating exhaustive matchingfrom enum import Enum
classColor(Enum):
RED = 1
GREEN = 2
BLUE = 3defdescribe_color(c):
match c:
case Color.RED:
return"Warm color"case Color.GREEN:
return"Cool color"case Color.BLUE:
return"Cool color"# No wildcard - if a new variant is added,# this will return None (implicit)for c in Color:
print(f"{c.name}: {describe_color(c)}")
# Case with walrus-like capture
data = {"type": "user", "name": "Alice", "age": 30, "role": "admin"}
match data:
case {"type": "user", "name": str(name), **rest}:
print(f"User {name}, extra fields: {rest}")
Output
Click "Run" to execute your code
Use ** in mapping patterns to capture remaining keys. For exhaustive matching with enums, omit the wildcard so that missing cases return None (or add explicit error handling).
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?