介绍
match case
flag = False
match (100, 200):
case (100, 300): # Mismatch: 200 != 300
print('Case 1')
case (100, 200) if flag: # Successful match, but guard fails
print('Case 2')
case (100, y): # Matches and binds y to 200
print(f'Case 3, y: {y}')
case _: # Pattern not attempted
print('Case 4, I match anything!')
方式二
def switch_case(value):
switcher = {
0: "zero",
1: "one",
2: "two",
}
return switcher.get(value, 'wrong value')
def foo(var,x):
return {
'a': lambda x: x+1,
'b': lambda x: x+2,
'c': lambda x: x+3,
}[var](x)