python 3.10 新增 switch-case结构用法

本文介绍了Python3.10中引入的match-case结构,一种类似其他编程语言switch-case的条件判断方式,它提供了更大的灵活性和代码清晰度,适用于状态机处理、类型判断、协议实现等多种场景。
摘要由CSDN通过智能技术生成

Switch 语句存在于很多编程语言中,早在 2016 年,PEP 3103 就被提出,建议 Python 支持 switch-case 语句。

PEP 3103 – 开关/案例声明icon-default.png?t=N7T8https://peps.python.org/pep-3103/

Python 3.10中的新增亮点:match-case结构解析

在Python 3.10之前,我们主要依赖if-elif-else语句来进行条件判断。,对于某些复杂的条件判断,if-elif-else结构可能无法提供足够的灵活性。例如,当需要根据多个条件进行组合判断时,或者当需要匹配特定的数据模式时,这种结构就显得力不从心。

2020 年,Python的创始人Guido van Rossum,提交了显示 switch 语句的第一个文档,命名为 Structural Pattern Matching,随着 Python 3.10 beta 版的发布,终于将 switch-case 语句纳入其中。Python 3.10版本的发布为开发者们带来了众多令人瞩目的新特性,其中match-case结构的引入无疑是最具吸引力的一项改进。这一新特性为Python提供了类似于其他语言中的switch-case语句的功能,使条件判断更加灵活和直观。

在 Python 中,这一功能由 match 关键词和 case 语句组成:

match subject:  
    case pattern1:  
        # 执行代码块1  
    case pattern2:  
        # 执行代码块2  
    case pattern3:  
        # 执行代码块3  
    ...  
    case _:  
        # 执行默认代码块

 subject是要进行匹配的对象,pattern1pattern2pattern3等是要匹配的模式。当subject与某个模式匹配成功时,就会执行相应的代码块。如果subject与所有模式都不匹配,则会执行带有_通配符的默认代码块。

与if-elif-else结构相比,match-case结构使用关键词matchcase来明确标识条件判断的结构使得代码更加清晰并且可以更加灵活地处理各种复杂的条件判断。

match-case结构在多种场景下都能发挥巨大作用,以下是一些典型的应用场景:

  1. 状态机处理:在处理状态机时,需要根据当前状态执行不同的操作。使用match-case结构可以方便地根据状态进行条件判断,并执行相应的操作。

  2. 类型判断:在处理不同类型的数据时,可能需要根据数据类型执行不同的操作。match-case结构可以方便地根据数据类型进行匹配,并执行相应的代码块。

  3. 协议实现:在实现某种协议时,需要根据协议的不同字段进行不同的处理。match-case结构可以方便地根据字段的值进行匹配,并执行相应的操作。

  4. 模式匹配:当需要根据特定的模式进行匹配时,可以使用match-case结构结合正则表达式等模式匹配方式来实现。

在元组中使用:

point=(5,6)
match point:
    case (0, 0):
        print("Origin")
    case (0, y):
        print(f"Y={y}")
    case (x, 0):
        print(f"X={x}")
    case (x, y):
        print(f"X={x}, Y={y}")
    case _:
        raise ValueError("Not a point")

在类中使用:

class Point():
    def __init__(self,x,y):
        self.x = x
        self.y = y
 
def location(point):
    match point:
        case Point(x=0, y=0):
            print("Origin is the point's location.")
        case Point(x=0, y=y):
            print(f"Y={y} and the point is on the y-axis.")
        case Point(x=x, y=0):
            print(f"X={x} and the point is on the x-axis.")
        case Point():
            print("The point is located somewhere else on the plane.")
        case _:
            print("Not a point")
 
point = Point(0, 1)
location(point)

 if 子句模式:

point = Point(x=0,y=0)
match point:
    case Point(x=x, y=y) if x == y:
        print(f"The point is located on the diagonal Y=X at {x}.")
    case Point(x=x, y=y):
        print(f"Point is not on the diagonal.")

 复杂模式和通配符:

def func(person):
    match person:
        case (name,"teacher"):
            print(f"{name} is a teacher.")
        case (name, _, "male"):
            print(f"{name} is man.")
        case (name, _, "female"):
            print(f"{name} is woman.")
        case (name, age, gender):
            print(f"{name} is {age} old.")
 
func(("Sam", "teacher"))
func(("John", 25, "male"))
func(("John", 25, "man"))
func(["John", 25, "female"])

 匿名函数实现:

def foo(var,x):
	return {
			'a': lambda x: x+1,
			'b': lambda x: x+2,
			'c': lambda x: x+3,	
	}[var](x)

与其他语言的比较

C语言:

switch (expression) {
    case constant-expression :
       statement(s);
       break; /* 可选的 */
    case constant-expression :
       statement(s);
       break; /* 可选的 */
    /* 您可以有任意数量的 case 语句 */
    default : /* 可选的 */
       statement(s);
}

 java:

switch (expression) {  
    case constant1:  
        // 代码块1  
        break; // 可选  
    case constant2:  
        // 代码块2  
        break; // 可选  
    // ... 可以有更多的 case 分支  
    default:  
        // 默认执行的代码块  
        break; // 可选  
}

Python中的switch-case支持多种模式匹配方式,并且可以与Python的其他特性(如类、函数等)无缝集成,提供更加丰富的功能,与一些其他语言中的switch-case结构相比,Python的match-case结构在语法上更加简洁和直观。它不需要使用break语句来跳出case分支,也支持使用通配符_来处理默认情况。

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中二电工吹短笛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值