python3 match case

since python 3.10, it support switch and case. it even can check on type and value

def check(v):
    match v:
        case "1", "2":
            print('both 1 2 is string')
        case "1", 2:
            print('1 is string 2 is int')
        case a, b:
            print(f'a={a}, b={b}')
        case a:
            print(f'a=a')

check(("1", "2"))
# both 1 2 is string

check(("1", 2))
# 1 is string 2 is int

check((1, None))
# a=1, b=None

check(1)
# a=a

Last updated