在 Python 3.10 及以后的版本中引入了
match...case 语句,它和其他编程语言里的
switch...case 语句有相似之处,但也存在不少区别,
语法结构
match...case(Python)match语句后面紧跟一个需要匹配的表达式,然后通过一系列case子句来定义匹配模式和对应的操作。case子句可以使用多种模式,如常量、变量、序列、映射等。- 示例代码如下:
def http_error(status):
match status:
case 400:
return "Bad request"
case 401 | 403:
return "Not allowed"
case 404:
return "Not found"
case _:
return "Something else"
print(http_error(404))

最低0.47元/天 解锁文章

1686

被折叠的 条评论
为什么被折叠?



