Python 3.10 Preview Release New Features

Python 3.10 Preview Release new Features - iLab Academy


The preview release of Python 3.10 was released on May 2, 2021. It can be downloaded here. But this is not a Stable release. Because the specialty here is the addition of a lot of new features. With the release of Python 3.10 stable release, there is no doubt that users will get a new experience. Let’s talk about this one by one.

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 release
SyntaxErrors:

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 Modules

No new module has been created for this Release. And The following modules have been improved.
asyncioargparse
arraybase64
bdbcodecs
collections.abccontextlib
cursesdataclasses
distutilsdoctest
encodingsenum
fileinputgc
globhashlib
hmacimportlib.metadata
inspectlinecache
ospathlib
platformpprint
py_compilepyclbr
shelvestatistics
sitesocket
sslsqlite3
sys_thread
threadingtraceback
typestyping
unittesturllib.parse

in The Last Lesson, We are talking about How To install Python on windows

Post a Comment

0 Comments