The match-case statement in Python is conceptually similar to the switch-case statement found in many other programming languages. Both are used to control the flow of a program by comparing a single expression against multiple possible patterns or values and executing different blocks of code based on which pattern or value matches.
match subject:
case pattern if condition:
# code to execute if pattern matches and condition is True
case pattern1:
# code to execute if pattern1 matches
case pattern2:
# code to execute if pattern2 matches
...
case _:
# code to execute if no other pattern matches
if
condition appended to a case
pattern. The code block for that case is executed only if the pattern matches and the guard condition evaluates to True
.|
operator to match multiple literals in a single case._
as a wildcard to handle any value not explicitly matched by previous cases.def describe_status_code(code):
match code:
case 200:
return "OK"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
case _:
return "Unknown status code"
def greet(name):
match name:
case "Alice":
return "Hello, Alice!"
case "Bob":
return "Hello, Bob!"
case _:
return "Hello, stranger!"
def classify_number(number):
match number:
case 0 | 1 | 2:
print("Small number")
case 3 | 4 | 5:
print("Medium number")
case _:
print("Large number")
def describe_number(num):
match num:
case x:
return f"The number is {x}"
def process_value(value):
match value:
case x if x > 0:
return f"Positive value: {x}"
case x if x < 0:
return f"Negative value: {x}"
case 0:
return "Zero"
def process_sequence(seq):
match seq:
case [x, y]:
return f"Sequence of two elements: {x}, {y}"
case [x, y, z]:
return f"Sequence of three elements: {x}, {y}, {z}"
case _:
return "Other sequence"
def process_tuple(tpl):
match tpl:
case (a, b):
return f"Two-element tuple: {a}, {b}"
case (a, b, c):
return f"Three-element tuple: {a}, {b}, {c}"
case _:
return "Other tuple"
def describe_person(person):
match person:
case {"name": name, "age": age}:
return f"Name: {name}, Age: {age}"
case _:
return "Unknown person"
__match_args__
attribute in the class definition, which is a tuple that specifies which attributes should be considered positional for the purposes of pattern matching.class Point:
__match_args__ = ("x", "y")
def __init__(self, x, y):
self.x = x
self.y = y
def describe_point(point):
match point:
case Point(x, y):
return f"Point with coordinates: ({x}, {y})"
case _:
return "Unknown point"
p = Point(1, 2)
print(describe_point(p))
# Output: Point with coordinates: (1, 2)
from enum import Enum
class TrafficLight(Enum):
RED = "Stop"
YELLOW = "Proceed with caution"
GREEN = "Go"
def describe_traffic_light(color):
match color:
case TrafficLight.RED:
return TrafficLight.RED.value
case TrafficLight.YELLOW:
return TrafficLight.YELLOW.value
case TrafficLight.GREEN:
return TrafficLight.GREEN.value
case _:
return "Unknown traffic light"