Release Python 3.10.0
One big new thing is the new switch/case statements version on steroid💪
It's called Structural Pattern Matching
A quick test:
A nice short way to specify an
Better error messages
One big new thing is the new switch/case statements version on steroid💪
It's called Structural Pattern Matching
A quick test:
vehicle = 'Taxi' match vehicle: case 'Car': print('Drive yourself') case 'Bus'|'Taxi': print('Public transport') case _: print(f'{vehicle!r} not in record'
Output:Public transport
status_code = 200 match status_code: case 400|401|403 : print("Bad request") case 200: print("Request Ok") case _: print("Something else bad happened")
Output:Request Ok
So as excepted and |
is used as as or
pattern.A nice short way to specify an
isinstance()
check.my_string = 'Hello' #my_string = 5 match my_string: case int(): print("I'm a integer") case str(): print("I'm a string")
Output:I'm a string
Better error messages
>>> lst = [1, 2, 3 File "<stdin>", line 1 lst = [1, 2, 3 ^ SyntaxError: '[' was never closed
>>> foo(x, z for z in range(10), t, w) File "<stdin>", line 1 foo(x, z for z in range(10), t, w) ^^^^^^^^^^^^^^^^^^^^ SyntaxError: Generator expression must be parenthesized
def foo(): if pin: x = 2 else: x = 100
Output: File "C:\Python310\indent.py", line 3
x = 2
^
IndentationError: expected an indented block after 'if' statement on line 2
Advice when error,eg typo ect...>>> liist('hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'liist' is not defined. Did you mean: 'list'? >>> list('hello') ['h', 'e', 'l', 'l', 'o']
>>> from datetime import date >>> >>> data(2020, 5, 17)) File "<stdin>", line 1 data(2020, 5, 17)) ^ SyntaxError: unmatched ')' >>> data(2020, 5, 17) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'data' is not defined. Did you mean: 'date'? >>> date(2020, 5, 17) datetime.date(2020, 5, 17)