defhttp_error(s): match s: case400: return"Bad request" case401 | 402: return"Not allowed" case _: return"something wrong" classpoint: x:int y:int defwhere_is(point): match point: case (0, 0): print("origin") case (0, y): print(f"Y={y}") case (x, y): print(f"X={x}, Y={y}") case _: print("Not a point")
defwhere_is(point): match point: case Point(x=0, y=0): print("origin") case Point(x=0, y=y): print(f"Y={y}") case Point(): print("somewhere else") case _: print("Not a point")
Pass
充当语句的占位,不执行任何操作。
1 2 3 4 5 6 7 8 9 10 11
whileTrue: pass
classEmptyClass: # 常用于创建一个最小的类 pass
defEmptyFunc(*args): # 函数的占位符 pass
ifTrue: pass
Function
函数没有 return 语句时,则默认返回 None。
默认值参数
1 2 3 4 5 6 7 8
deff(a, L=None): if L isNone: L = [] L.append(a) return L