📜  结构模式匹配python代码示例

📅  最后修改于: 2022-03-11 14:45:46.392000             🧑  作者: Mango

代码示例1
# Structural pattern matching allows you to write switch/case statements
# in Python. You need to have Python 3.10 to use this feature.
x = 10
match x:
    case 10:
        print('x = 10') # if x is 10, this will print 10
    case 20:
        print('x = 20') # if x is 20, this will print 20
    case _:
        # The underscore allows you to write a default case if none of the
        # conditions above are true.
        print(f'x = {x}')