Introduction
The match-case
statement is a feature introduced in Python 3.10 for pattern matching. It allows you to match values against patterns and execute code based on which pattern matches. This can be particularly useful for complex branching logic, making your code more readable and expressive.
Basic Syntax
The basic syntax of the match-case
statement is as follows:
match value:
case pattern1:
# code to execute if value matches pattern1
case pattern2:
# code to execute if value matches pattern2
case _:
# code to execute if value matches no patterns (default case)
Examples
Example 1: Simple Match
def http_status(code):
match code:
case 200:
return "OK"
case 404:
return "Not Found"
case 500:
return "Internal Server Error"
case _:
return "Unknown Status"
print(http_status(200)) # Output: OK
print(http_status(404)) # Output: Not Found
print(http_status(123)) # Output: Unknown Status
Example 2: Matching with Variables
You can use variables in patterns to match against values and capture parts of the value.
def describe_point(point):
match point:
case (0, 0):
return "Origin"
case (x, 0):
return f"X-axis at {x}"
case (0, y):
return f"Y-axis at {y}"
case (x, y):
return f"Point at ({x}, {y})"
case _:
return "Not a point"
print(describe_point((0, 0))) # Output: Origin
print(describe_point((3, 0))) # Output: X-axis at 3
print(describe_point((0, 4))) # Output: Y-axis at 4
print(describe_point((2, 5))) # Output: Point at (2, 5)
print(describe_point("Hello")) # Output: Not a point
Example 3: Matching Class Instances
You can match against class instances and their attributes.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def describe_object(obj):
match obj:
case Point(0, 0):
return "Origin"
case Point(x, 0):
return f"X-axis at {x}"
case Point(0, y):
return f"Y-axis at {y}"
case Point(x, y):
return f"Point at ({x}, {y})"
case _:
return "Unknown object"
p1 = Point(0, 0)
p2 = Point(3, 0)
p3 = Point(0, 4)
p4 = Point(2, 5)
p5 = "Hello"
print(describe_object(p1)) # Output: Origin
print(describe_object(p2)) # Output: X-axis at 3
print(describe_object(p3)) # Output: Y-axis at 4
print(describe_object(p4)) # Output: Point at (2, 5)
print(describe_object(p5)) # Output: Unknown object
Example 4: Using Guards
You can add an if
condition (guard) to a pattern to make the match more specific.
def number_type(n):
match n:
case x if x < 0:
return "Negative"
case x if x == 0:
return "Zero"
case x if x > 0:
return "Positive"
case _:
return "Unknown"
print(number_type(-5)) # Output: Negative
print(number_type(0)) # Output: Zero
print(number_type(10)) # Output: Positive
Example 5: Combining Patterns
You can combine multiple patterns using the |
operator.
def weekend_or_weekday(day):
match day:
case "Saturday" | "Sunday":
return "Weekend"
case "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday":
return "Weekday"
case _:
return "Unknown"
print(weekend_or_weekday("Monday")) # Output: Weekday
print(weekend_or_weekday("Saturday")) # Output: Weekend
print(weekend_or_weekday("Holiday")) # Output: Unknown
Conclusion
The match-case
statement in Python 3.10 introduces powerful pattern matching capabilities, allowing for more readable and maintainable branching logic. By using patterns, variables, class instances, guards, and combining patterns, you can handle complex conditions more effectively. Understanding and utilizing the match-case
statement will enable you to write cleaner and more expressive Python code.