Python: 5 new features and functions that version 3.10 brings

March 22, 2022

Tags: Technologies
python
Unsplash

 

Python is one of the most popular programming languages ​​at the moment. Due to its versatility, it is used by people outside the development of websites and applications, this has been the main reason for its popularity.

 

People within different fields use Python for multiple tasks such as data analysis and visualization, creating artificial intelligence, and automating different machines, perhaps the latter being its most popular use.

 

Other uses that are given to Python, among people who are not developers, are: automating the actions of copying and pasting files and folders, uploading them to a server. Even with Python, you can automate your tasks in Excel, PDF, and CSV files.

 

Now, this versatile programming language is launching its new version, 3.10, and in this blog, we will talk a little about it.

 

Python 3.10: what this new update brings

 

On their official website, they detailed the new features and functions that come with this update, including:

 

Context Managers in Parentheses

 

Using parentheses for continuation on multiple lines is now supported in context managers. This allows a long collection of context managers to be formatted on multiple lines in a similar way as was previously possible with import statements. For example, all of these examples are now valid:

 

with (CtxManager() as example):
    ...

with (
    CtxManager1(),
    CtxManager2()
):
    ...

with (CtxManager1() as example,
      CtxManager2()):
    ...

with (CtxManager1(),
      CtxManager2() as example):
    ...

with (
    CtxManager1() as example1,
    CtxManager2() as example2
):
    ...

 

It is also possible to use a trailing comma at the end of the enclosing group:

 

with (
    CtxManager1() as example1,
    CtxManager2() as example2,
    CtxManager3() as example3,
):
    ...

 

Improved syntax error messages

 

One of the improvements, and one that has been celebrated by the experts of this language, was that now when parsing code that contains unclosed parentheses or square brackets, it includes the location instead of showing unexpected SyntaxError:EOF when pointing to an incorrect location. For example:

 

expected = {9:1, 18:2, 19:2, 27:3, 28:3, 29:3, 36:4, 37:4,
            38:4, 39:4, 45:5, 46:5, 47:5, 48:5, 49:5, 54:6,
some_other_code = foo()

 

In Python 3.10, better error reporting is issued

 

File "example.py", line 1
    expected = {9:1, 18:2, 19:2, 27:3, 28:3, 29:3, 36:4, 37:4,
               ^
SyntaxError: '{' was never closed

 

Indentation errors

 

Now, with Python 3.10, the IndentationError exceptions are more verbose regarding what type of block was expecting an indentation:

 

>>> def foo():
... if lel:
... x = 2
  File "<stdin>", line 3
    x = 2
    ^
IndentationError: expected an indented block after 'if' statement in line 2

 

Attribute errors

 

On the official Python page, they explain "when printing AttributeError, PyErr_Display() will offer suggestions of similar attribute names in the object from which the exception was thrown" They offer this example:

 

>>> collections.namedtoplo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?

 

Name errors

 

With this update, PyErr_Display() now offers similar variable name suggestions in the function where a name error 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?

 

These are the most notable new features coming with the Python 3.10 update. On the official page of the popular programming language, they detail each of its new features, making the development of web pages and automation processes easier for experts in this language.

 

We recommend you on video

 

Yes, I liked thiscontent.