Python 3.11 is Coming! Here’s How It Fares Against Python 3.10 | by Eldad Uzman | Apr, 2022

Photograph by Francesco Gallarotti on Unsplash

Based on the Python Software program Basis (PSF), Python 3.11 is in it’s seventh alpha revision and is deliberate to be launched in October 2022.

What modifications are included for the upcoming model?

To judge the variations between model 3.10 and three.11, I’ve arrange 2 docker containers.

First container for model 3.10:

docker run -t -d python:3.10.4-bullseye

Second container for model 3.11:

docker run -t -d python:3.11-rc-bullseye

Having the 2 containers working I can use vs code remote container to connect to the working containers.

Then I can execute my Python code within the two environments and see the distinction.

Within the following sections, I’ll first present a code instance, after which I’ll present the distinction between the 2 variations.

Output in model 3.10:

1
100
Traceback (most up-to-date name final):
File "/usr/native/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/native/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/root/py310/myapp/__main__.py", line 4, in <module>
print(d["key_11"])
KeyError: 'key_11'

Output in model 3.11

1
100
Traceback (most up-to-date name final):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/root/py311/myapp/__main__.py", line 4, in <module>
print(d["key_11"])
~^^^^^^^^^^
KeyError: 'key_11'

Python 3.11 discloses a greater error location to the developer making an superior developer expertise.

The self sort has already been launched within the typing extensions module and now it’s promoted to the usual typing library.

The code above represents the construction of a listing. Directories have subdirectories so the definition is recursive.

Output in Python 3.10:

Traceback (most up-to-date name final):
File "/usr/native/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/native/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/root/py310/myapp/__main__.py", line 2, in <module>
from typing import Record, Tuple, Self

Output in Python 3.11:

'content material': (['a.txt', 'b.txt'],
['content': (['file1', 'file2'], None), 'title': 'dir1']),
'title': 'dir2'

Observe: You possibly can annotate the kind of the subdir by the title of the category itself. But when the mother or father class title modifications you’d have to vary all of the annotation references accordingly.

To ensure that this code to work in 3.11 and three.10 you may carry out importing as comply with:

attempt:
from typing import Self
besides ImportError:
from typing_extensions import Self

The BaseException class now has a __note__ class attribute defaults to None.
You possibly can override it with any string of your alternative and supply additional data.

Output in Python 3.10:

Traceback (most up-to-date name final):
File "/usr/native/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/native/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/root/py310/myapp/__main__.py", line 6, in <module>
increase MyException("some exception")
__main__.MyException: some exception

Output in Python 3.11:

Traceback (most up-to-date name final):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/root/py311/myapp/__main__.py", line 6, in <module>
increase MyException("some exception")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MyException: some exception
that is my word :)

We see that the word has been added to the output and it permits builders to be extra communicative of their exceptions.

Model 3.11 introduces a brand new exception sort title ExceptionGroup to throw a bundle of exceptions and deal with them in an besides clause.

Along with {that a} new besides* syntax is launched.

Output in Python 3.10:

File "/usr/native/lib/python3.10/runpy.py", line 189, in _run_module_as_main
mod_name, mod_spec, code = _get_main_module_details(_Error)
File "/usr/native/lib/python3.10/runpy.py", line 223, in _get_main_module_details
return _get_module_details(main_name)
File "/usr/native/lib/python3.10/runpy.py", line 157, in _get_module_details
code = loader.get_code(mod_name)
File "<frozen importlib._bootstrap_external>", line 1017, in get_code
File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/root/py310/myapp/__main__.py", line 18
besides * (ToYoungException, EmailIsInvalidException) as exception_group_1:

Output in Python 3.11:

validations failed
+ Exception Group Traceback (most up-to-date name final):
| File "<frozen runpy>", line 198, in _run_module_as_main
| File "<frozen runpy>", line 88, in _run_code
| ExceptionGroup: (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most up-to-date name final):
| File "/root/py311/myapp/__main__.py", line 14, in <module>
| increase ExceptionGroup(
| ^^^^^^^^^^^^^^^^^^^^^
| ExceptionGroup: Knowledge validations (2 sub-exceptions)
+-+---------------- 1 ----------------
| ToYoungException: Age have to be over 18 - age is 11
+---------------- 2 ----------------
| EmailIsInvalidException: E mail have to be legitimate some_wannabe_email
+------------------------------------
|
| The above exception was the direct reason for the next exception:
|
| Traceback (most up-to-date name final):
| File "/root/py311/myapp/__main__.py", line 20, in <module>
| increase ValueError from exception_group_1
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ValueError
+------------------------------------

As we are able to see, it’s extremely worthwhile when we have now a number of failure causes which we wish to disclose in a single go.

Output in Python 3.10:

Traceback (most up-to-date name final):
File "/usr/native/lib/python3.10/runpy.py", line 189, in _run_module_as_main
mod_name, mod_spec, code = _get_main_module_details(_Error)
File "/usr/native/lib/python3.10/runpy.py", line 223, in _get_main_module_details
return _get_module_details(main_name)
File "/usr/native/lib/python3.10/runpy.py", line 157, in _get_module_details
code = loader.get_code(mod_name)
File "<frozen importlib._bootstrap_external>", line 1017, in get_code
File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/root/py310/myapp/__main__.py", line 11
return n: [x async for x in elements(n)] for n in vary(3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: asynchronous comprehension exterior of an asynchronous perform

Output in Python 3.11:

0: [1], 1: [1, 1], 2: [1, 2, 4], 3: [1, 3, 9, 27], 4: [1, 4, 16, 64, 256]

The second the code enters a comprehension block it is now conscious of the comprehension as to its present “perform context”.
If the comprehension shouldn’t be asynchronous then the interior code block cannot have asynchronous statements.

In Python 3.11 although, comprehensions turn into implicitly asynchronous in the event that they include interior async statements that enables for interior async comprehensions.

TOML parsing is now a part of the usual library identical to JSON and CSV.

Output in Python 3.10:

Traceback (most up-to-date name final):
File "/usr/native/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/native/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/root/py310/myapp/__main__.py", line 2, in <module>
import tomllib
ModuleNotFoundError: No module named 'tomllib'

Output in Python 3.11:

'shoppers': 'information': [['gamma', 'delta'], [1, 2]], 'hosts': ['alpha', 'omega'],
'database': 'connection_max': 5000,
'enabled': True,
'ports': [8000, 8001, 8002],
'server': '192.168.1.1',
'proprietor': 'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=57600))),
'title': 'Tom Preston-Werner',
'servers': 'alpha': 'dc': 'eqdc10', 'ip': '10.0.0.1',
'beta': 'dc': 'eqdc10', 'ip': '10.0.0.2',
'title': 'TOML Instance'

It’s argued that Python 3.11 is 10–60% quicker in run time in comparison with python 3.10. You possibly can learn extra about it within the benchmarking part here.

More Posts