python match case语法

学习路线:B站

普通的if判断

def if_traffic_light(color):
    if color == 'red':
        return 'Stop'
    elif color == 'yellow':
        return 'Slow down'
    elif color == 'green':
        return 'Go'
    else:
        return 'Invalid color'

print(if_traffic_light('red'))  # Output: Stop
print(if_traffic_light('yellow')) # Output: Slow down
print(if_traffic_light('green')) # Output: Go
print(if_traffic_light('blue')) # Output: Invalid color

使用match-case方式,同样达到效果:

def match_case_1(color):
    match color:
        case 'red':
            return 'Stop'
        case 'green':
            return 'Go'
        case 'yellow':
            return 'Caution'
        case _:
            return 'Invalid color'
print(match_case_1('red'))
print(match_case_1('green'))
print(match_case_1('yellow'))

match-case 解包

普通的if语句:

def if_point(point: tuple):
    if len(point) == 2:
        if point[0] == 0 and point[1] == 0:
            print("origin")
        else:
            print(f"x={point[0]}, y={point[1]}")
    else:
        print("invalid point")

使用match-case改进:

# 使用match-case 解包
def match_case_1(point: tuple):
    """
    使用match-case解包, 解包后可以使用变量名来访问元组中的元素,
    可以使用通配符_来匹配任意值
    """
    match point:
        case (0, 0):
            print(f"Origin")
        case (x, y):
            print(f"x={x}, y={y}")
        case other: # 也可以用下划线代替
            print(f"{other}Unknown point")

match_case_1((1, 2))  # x=1, y=2
match_case_1((0, 0))  # Origin
match_case_1((1,))  # (1,)Unknown point
match_case_1((1, 2, 3))  # (1, 2, 3)Unknown point

match-case 解包升级

# 使用match-case 解包
def match_case_1(point: tuple):
    """
    使用match-case解包, 解包后可以使用变量名来访问元组中的元素,
    可以使用通配符_来匹配任意值
    """
    match point:
        case (0, 0):
            print(f"Origin")
        case (x, 0):
            print(f"on x-axis  x={x}, y=0")
        case (0, y):
            print(f"on y-axis x=0, y={y}")
        case (x, y):
            print(f"x={x}, y={y}")
        case other:
            print(f"{other}Unknown point")


match_case_1((0, 1))  # on y-axis x=0, y=1
match_case_1((2,0))  # on x-axis  x=2, y=0
match_case_1((1, 2, 3))  # (1, 2, 3)Unknown point 

match-case 解包支持逻辑判断

# 使用match-case 解包
def match_case_1(point: tuple):
    """
    使用match-case解包, 解包后可以使用变量名来访问元组中的元素,
    可以使用通配符_来匹配任意值
    """
    match point:
        case (0, 0):
            print(f"Origin")
        case (x, 0) | (0, x): # 这里是或逻辑
            print(f"on axis")
        case (x, y):
            print(f"x={x}, y={y}")
        case other:
            print(f"{other}Unknown point")


match_case_1((0, 1))  # on y-axis x=0, y=1
match_case_1((2,0))  # on x-axis  x=2, y=0
match_case_1((1, 2, 3))  # (1, 2, 3)Unknown point

match case 不会区分序列的类型,tuple与list是一样的

my_tuple = (1, 2)
my_list = [1, 2]
my_tuple = (1, 2)
my_list = [1, 2]

match my_tuple:
    case 1, 2: # 可以匹配上
        print("Tuple matches (1, 2)")

match my_list:
    case 1, 2: # 可以匹配上
        print("List matches [1, 2]")
 
match my_tuple:
    case (1, 2): # 可以匹配上
        print("Tuple matches (1, 2)")

match my_list:
    case [1, 2]: # 同样可以匹配
        print("List matches [1, 2]")

如果需要区分类型,则可以修改:

my_tuple = (1, 2)
my_list = [1, 2]

match my_list:
    case tuple((1, 2)): # 无法匹配
        print("Tuple matches (1, 2)")

match my_tuple:
    case tuple((1, 2)): # 可以匹配
        print("Tuple matches (1, 2)")

进一步的,match case 也可以匹配类型,而不必关心具体内容:

my_tuple = (1, 2)
my_list = [1, 2]

match my_tuple:
    case tuple():
        print("Tuple match")
    case list():
        print("List match")

match case 支持条件判断

def match_quadrant(point):
    match point:
        case (x, y) if x > 0 and y > 0:
            return "First quadrant"
        case (x, y) if x < 0 and y > 0:
            return "Second quadrant"
        case (x, y) if x < 0 and y < 0:
            return "Third quadrant"
        case (x, y) if x > 0 and y < 0:
            return "Fourth quadrant"
        case (0, _) | (_, 0):
            return "On the axis"
        case _:
            return "Invalid point"

point1 = (5, 3)
point2 = (-2, 7)
print(match_quadrant(point1))
print(match_quadrant(point2))

match case 字典匹配

dict_p = {'x': 1, 'y': 2}

match dict_p:
    case {'x': 0, 'y': 0}:
        print('origin')
    case {'x': x, 'y': y}:
        print(f'x={x}, y={y}')

也可以部分匹配:

dict_p = {'x': 1, 'y': 2}

match dict_p:
    case {'x': 0, 'y': 0}:
        print('origin')
    case {'x':1 }:
        print(f'x={x}, y={y}')

match case 匹配自定义类

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
p = Point(1, 2)
match p:
    # 这里的case匹配的是p的类型和值,值必须是关键字参数
    # 否则报错:TypeError: Point() accepts 0 positional sub-patterns (2 given)
    case Point(x=1, y=2):
        print("Point is (1, 2)")
    case Point(x=3, y=4):
        print("Point is (3, 4)")
    case _:
        print("Point is not (1, 2) or (3, 4)")

如果想要在case中使用位置参数,需要添加类变量__match_args__ = ("x", "y")

class Point:
	# 指定macth case中的位置参数
    __match_args__ = ("x", "y")
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    
p = Point(1, 2)
match p:
    # 这里的case匹配的是p的类型和值,值必须是关键字参数
    case Point(x, y):
        print("Point is (1, 2)")
    case Point(x=3, y=4):
        print("Point is (3, 4)")
    case _:
        print("Point is not (1, 2) or (3, 4)")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值