defhttp_error(status): match status: case400: return"Bad request" case404: return"Not found" case418: return"I'm a teapot" case455 | 456: return"This is a custom error" case _: return"Something's wrong with the Internet"
# I'm a teapot # This is a custom error # This is a custom error # Something's wrong with the Internet
带有字面值和变量的模式
1 2 3 4 5 6 7 8 9 10 11 12
# point is an (x, y) tuple match point: case (0, 0): print("Origin") case (0, y): print(f"Y={y}") case (x, 0): print(f"X={x}") case (x, y): print(f"X={x}, Y={y}") case _: raise ValueError("Not a point")
模式和类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classPoint: x: int y: int
deflocation(point): match point: case Point(x=0, y=0): print("Origin is the point's location.") case Point(x=0, y=y): print(f"Y={y} and the point is on the y-axis.") case Point(x=x, y=0): print(f"X={x} and the point is on the x-axis.") case Point(): print("The point is located somewhere else on the plane.") case _: print("Not a point")
约束项
1 2 3 4 5
match point: case Point(x, y) if x == y: print(f"The point is located on the diagonal Y=X at {x}.") case Point(x, y): print(f"Point is not on the diagonal.")
复杂模式和通配符
1 2 3 4 5
match test_variable: case ('warning', code, 40): print("A warning has been received.") case ('error', code, _): print(f"An error {code} occurred.")
更好的错误信息
语法错误(SynaxErrors)
字典未关闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
test_dict = {1: 1, 2: 2, 3: 3
# Python3.9 """ File "/home/stolen/test.py", line 2 ^ SyntaxError: unexpected EOF while parsing """
# Python3.10 """ File "/home/stolen/test.py", line 1 test_dict = {1: 1, 2: 2, 3: 3 ^ SyntaxError: '{' was never closed """
冒号缺失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if1 > 2 print('abc')
# Python3.9 """ File "/home/stolen/test.py", line 1 if 1 > 2 ^ SyntaxError: invalid syntax """
# Python3.10 """ File "/home/stolen/test.py", line 1 if 1 > 2 ^ SyntaxError: expected ':' """
判断等于错误
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
ifbool(3.14) = True: print('true')
# Python3.9 """ File "/home/stolen/test.py", line 1 if bool(3.14) = True: ^ SyntaxError: invalid syntax """
# Python3.10 """ File "/home/stolen/test.py", line 1 if bool(3.14) = True: ^^^^^^^^^^ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? """
缩进错误(IndentationErrors)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
ifbool(3.14) == True: print('true')
# Python3.9 """ File "/home/project/demo.py", line 2 print('true') ^ IndentationError: expected an indented block """
# Python3.10 """ File "/home/project/demo.py", line 2 print('true') ^ IndentationError: expected an indented block after 'if' statement on line 1 """
属性错误(AttributeErrors)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
a = 'abc' a.isnumerics()
# Python3.9 """ Traceback (most recent call last): File "/home/project/demo.py", line 2, in <module> a.isnumerics() AttributeError: 'str' object has no attribute 'isnumerics' """
# Python3.10 """ Traceback (most recent call last): File "/home/project/demo.py", line 2, in <module> a.isnumerics() AttributeError: 'str' object has no attribute 'isnumerics'. Did you mean: 'isnumeric'? """
命名错误(NameErrors)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
hello = 'hello, world!' print(hallo)
# Python3.9 """ Traceback (most recent call last): File "/home/project/demo.py", line 2, in <module> print(hallo) NameError: name 'hallo' is not defined """
# Python3.10 """ Traceback (most recent call last): File "/home/project/demo.py", line 2, in <module> print(hallo) NameError: name 'hallo' is not defined. Did you mean: 'hello'? """