Python match...case Statement
match...caseIt provides a more powerful pattern matching method.
Pattern matching is a way of processing data structures in programming, making code more concise and readable.
match...caseIt is new syntax introduced in Python 3.10.
match...caseThe syntax structure is as follows:
match expression:
case pattern1:
# 处理pattern1的逻辑
case pattern2 if condition:
# 处理pattern2并且满足condition的逻辑
case _:
# 处理其他情况的逻辑
Parameter description:
matchThe statement is followed by an expression, then usecasestatement to define different patterns.caseIt is followed by a pattern, which can be a specific value, variable, wildcard, etc.- You can use
ifkeyword incaseto add conditions. _It is usually used as a wildcard to match any value.

Example
1. Simple Value Matching
Example
def match_example(value):
match value:
case 1:
print("Matched value 1")
case 2:
print("Matched value 2")
case _:
print("Matched other values")
match_example(1) # Output: Matched value 1
match_example(2) # Output: Matched value 2
match_example(3) # Output: Matched other values
match value:
case 1:
print("Matched value 1")
case 2:
print("Matched value 2")
case _:
print("Matched other values")
match_example(1) # Output: Matched value 1
match_example(2) # Output: Matched value 2
match_example(3) # Output: Matched other values
In the above code,matchthe statement is used to matchvaluedifferent cases, eachcasestatement represents a possible matching case,_The wildcard represents other cases.
The output result is:
匹配到值为1 匹配到值为2 匹配到其他值
2. Using Variables
Example
def match_example(item):
match item:
case (x, y) if x == y:
print(f"Matched an equal tuple: {item}")
case (x, y):
print(f"Matched a tuple: {item}")
case _:
print("Matched other cases")
match_example((1, 1)) #Output:Matched an equal tuple: (1, 1)
match_example((1, 2)) #Output:Matched a tuple: (1, 2)
match_example("other") #Output:Matched other cases
match item:
case (x, y) if x == y:
print(f"Matched an equal tuple: {item}")
case (x, y):
print(f"Matched a tuple: {item}")
case _:
print("Matched other cases")
match_example((1, 1)) #Output:Matched an equal tuple: (1, 1)
match_example((1, 2)) #Output:Matched a tuple: (1, 2)
match_example("other") #Output:Matched other cases
The output result is:
匹配到相等的元组: (1, 1) 匹配到元组: (1, 2) 匹配到其他情况
3. Type Matching
Example
class Circle:
def __init__(self, radius):
self.radius = radius
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def match_shape(shape):
match shape:
case Circle(radius=1):
print("Matched a circle with radius 1")
case Rectangle(width=1, height=2):
print("Matched a rectangle with width 1 and height 2")
case _:
print("Matched other shapes")
match_shape(Circle(radius=1)) # Output: Matched a circle with radius 1
match_shape(Rectangle(width=1, height=2)) # Output: Matched a rectangle with width 1 and height 2
match_shape("other") # Output: Matched other shapes
def __init__(self, radius):
self.radius = radius
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def match_shape(shape):
match shape:
case Circle(radius=1):
print("Matched a circle with radius 1")
case Rectangle(width=1, height=2):
print("Matched a rectangle with width 1 and height 2")
case _:
print("Matched other shapes")
match_shape(Circle(radius=1)) # Output: Matched a circle with radius 1
match_shape(Rectangle(width=1, height=2)) # Output: Matched a rectangle with width 1 and height 2
match_shape("other") # Output: Matched other shapes
The output result is:
匹配到半径为1的圆 匹配到宽度为1,高度为2的矩形 匹配到其他形状Other Extensions
Python3 Conditional Control