Python的match-case语法

发布时间 2023-03-31 18:17:39作者: Clotho_Lee

Python 3.10版本在2021年10月发布,新增了match-case语法。其实就是对应别的开发语言的switch-case语法。

 

例子

def http_error(status):
    match status:
        case 400:
            print("Bad request")
        case 404:
            print("Not found")
        case 418:
            print("I'm a teapot")
        case _:
            print("Something's wrong with the internet")