Python 3.10 - Structural Pattern Matching
Structural pattern matching has been added in the form of a match statement and case statements of patterns with associated actions. Patterns consist of sequences, mappings, primitive data types as well as class instances. Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data.#Example
num = 10
match num:
case 1:
<action_1>
case 5:
<action_2>
case 10:
<action_3>
case _:
<action_wildcard> #Default Function
Python 3.10 Parenthesized context managers
Now You can use Brackets in the 'with' statement. Then you can write Context Managers as multiline. Using enclosing parentheses for continuation across multiple lines in context managers is now supported. This allows formatting a long collection of context managers in multiple lines in a similar way as it was previously possible with import statements.with (CtxManager() as example):
...
with (
CtxManager1(),
CtxManager2()
):
...
with (CtxManager1() as example,
CtxManager2()):
...
with (CtxManager1(),
CtxManager2() as example):
...
with (
CtxManager1() as example1,
CtxManager2() as example2
):
Better error messages
Python is a computer language that is known to display error messages in a very friendly manner compared to other languages. Below you can see how these have been made more sensitive in the Python 3.10 releaseSyntaxErrors:
Previously only the line with the syntax error was shown. But the new release shows its location correctly. Consider the EOF error (unopened {) in the following example.
expected = {9: 1, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
another_code = foo()
Error Message Should be like
File "example.py", line 3
another_code = foo()a
^
SyntaxError: invalid syntax
IndentationErrors
Indention Error: Briefly indicates the location of the function and where the indention was needed. See the example below>>> def foo():
... if condition:
... x = 2
File "<stdin>", line 3
x = 2
^
IndentationError: expected an indented block after 'if' statement in line 2
AttributeErrors
When AttributeError: appears, it suggests a similar Attribute name. See the example below.>>> collections.namedtoplo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?
NameErrors
It also suggests similar variables when a NameError occurs.
>>> schwarzschild_black_hole = None
>>> schwarschild_black_hole
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?
Python 3.10 New ModulesNo new module has been created for this Release. And The following modules have been improved.
asyncio | argparse |
array | base64 |
bdb | codecs |
collections.abc | contextlib |
curses | dataclasses |
distutils | doctest |
encodings | enum |
fileinput | gc |
glob | hashlib |
hmac | importlib.metadata |
inspect | linecache |
os | pathlib |
platform | pprint |
py_compile | pyclbr |
shelve | statistics |
site | socket |
ssl | sqlite3 |
sys | _thread |
threading | traceback |
types | typing |
unittest | urllib.parse |
0 Comments