logo

Get in touch

Awesome Image Awesome Image

Python Development September 8, 2022

The Python 3.11 release: everything you need to know

Written by Dharmesh Patel

29,477

As per Python Software Foundation (PSF) the release of Python 3.11, which is scheduled for October 2022, is currently in its seventh alpha revision.

Python is now regularly linked to the most significant advances in data science and AI. Practitioners value its simplicity, extensive collection of in-built and third-party libraries, and network. The Python community can anticipate a number of positive enhancements, including a considerable performance gain, better error detection and debug, and several quality-of-life upgrades to standard libraries, with the release of its latest version of 3.11 on October 3, 2022.

What modifications are made to the forthcoming version? Let’s have a look.

Python 3.11 Introduces New Features and Functionality

 The complete list of enhancements included in Python 3.11 is as follows:

General alterations

  • Improved Speed – The usual benchmark suite runs around 25% quicker than in 3.10, which is the first notable change that will thrill data scientists. According to the Python documentation, 3.11 can occasionally be up to 60% quicker. Here’s how to run the benchmark test to evaluate speed gains on your own computer. You will require a Docker installation in order to compare the speeds of Python 3.10 and 3.11.
  • Smooth Error Locations in Tracebacks for Easier Debugging – In the past, lines would be used to identify errors. The specific error will now be underlined on the line, removing any ambiguity and accelerating troubleshooting.
  •  Using Exception Groups and except, error handling is made easier – Previously, if a task produced numerous errors, you had to address each one separately. With version 3.11, you can now handle each of those problems separately, which streamlines exception handling.
  • Standard Library support for TOML parsing – A customization file format comparable to YAML is called TOML, or Tom’s Obvious Minimal Language. It includes reliable and consistent build information for your project. There is no longer any justification for using setup.py to package your project since 3.11 added tomllib to the standard library. 

Typing and Language Changes

  • Self Type – The annotation of self returns has been awkward for a long time, resulting in confusing results from your analysis tools. “typing.Self” simplifies the annotation process by allowing you to just annotate the return value.
  • Variadic Generics – Python supports type hints, which now include include TypeVarTuple. When you are anticipating a particular array structure, this lets you define a placeholder for tuples, which is useful.
  • Independent Literal String Type – Before, there was no method for type annotations to specify that a specific variable had to be a string defined in the source code. Linters can now test whether a variable is a string specified in the source code or a new string created using only strings stated in the source code using the new typing.LiteralString annotation.
  • TypedDict Required vs Missing Items – The declaration of some keys as needed and others as potentially absent is not supported by TypedDict at this time. 3.11 adds Required() and notrequired() to give you a mechanism to take these into account.

Python Portfolio CTA

Here is a detailed look at some of the major Python 3.11 features.

Let’s find out why there is so much talk in the market about the upcoming release of Python 3.11. Below mentioned are some of its crucial features that increase its value in the market.

  1. CPython Optimisation
  2. Better error messages
  3. Adding the new keyboard shortcut: Self
  4. Exception Notes
  5. Use except* to handle multiple exceptions
  6. Inclusion of A TOML Parser

CPython Optimisation

Python 3.11 is said to run 10–60% faster than Python 3.10 depending on the workload.

The standard version of the Python programming language is called CPython. The primary and most widely used Python interpreter is CPython, which was created in C and Python. The CPython interpreter is substantially more streamlined and swifter in version 3.11 compared to version 3.10. When constructed with GCC on Ubuntu Linux and tested using the performance benchmark set.

According to the documentation, the authors of Python 3.11 mainly concentrated on speedier startup and runtime.

Better error messages

Better error message that identifies the precise issue location is another intriguing new feature of Python 3.11. Python 3.11 identifies the precise expression that made the issue, as opposed to returning a 100-line traceback with a confusing error message.

unnamed (38)

In the above illustration, the Python interpreter identifies the x whose None value caused the script to crash. In the 

latest versions of Python, the mistake would be unclear because two objects contain the ‘x’ attribute. Nonetheless, the 3.11 error handling makes the faulty statement quite evident.

This example demonstrates how 3.11 can identify an error from a vocabulary that is deeply layered and make it clear to which key the error belongs.

unnamed (39)

Complex arithmetic expressions exhibit the same precision. 

unnamed (40)

Specifically for data scientists who have trouble debugging scripts, the revised error messages are a wonderful improvement.

Adding the new keyboard shortcut: Self

Self Type, which is one of the Python great features that enable users to describe functions, is included in this version. You can add metadata to function inputs and return values using the function annotation feature. The input type of the function’s parameters and the instance variables of the value the function returns can both be specified in this manner.

Consider the case where we have a class called School and the function get school returns a School instance. Before, we would have had to use TypeVar, which is rather verbose, if we wanted to define the function.

from typing import TypeVar
TSchool = TypeVar('TSchool', bound='School')
 class School:
     def get_school(self: TSchool) -> TSchool:
     return self

Version 3.11 allows us to just use the Self type, which is far more logical and straightforward to code.

from typing import Self
class School:
     def get_school(self)
-> Self:
     return self

Exception Notes

With the addition of notes to exception objects, which are kept with the exceptions and shown when the exception is created, Python 3.11 makes tracebacks even more context-rich.

As an illustration, consider the following code, which includes crucial details about certain API data translation logic:

def get_seconds(data):
try:
     milliseconds = float(data['milliseconds'])
except ValueError as exc:
     exc.add_note(
         "The time field should always be a number, this is a critial bug. "
         "Please report this to the backend team immediately."
     )
     raise  # re-raises the exception, instead of silencing it
seconds = milliseconds / 1000
return seconds

get_seconds({‘milliseconds’: ‘foo’})  # ‘foo’ is not a number!

Just below the Exception message is printed the following additional note: 

Traceback (most recent call last):
  File "asd.py", line 14, in <module>
get_seconds({"milliseconds": "foo"})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "asd.py", line 3, in get_seconds
milliseconds = float(data["milliseconds"])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: 'foo'
The time field should always be a number, this is a critical bug. Please report this to the backend team immediately. 

Use except* to handle multiple exceptions

Dealing with numerous exceptions is a fascinating new capability added in this edition. By utilizing the except* keyword and the ExceptionGroup class, we will be able to raise many exceptions at once. Here is an illustration: 

try:
       raise ExceptionGroup("Exception Group for multiple errors", (
           ValueError("This is a value error"),
           TypeError("This is a type error"),
           KeyError("This is a Key error"),
AttributeError('This is an Attribute Error')
       ))
   # Different ways of handling errors
   except* AttributeError: 
       ...
   except* (ValueError, TypeError) as exc:
       ...
   except* KeyError as exc:
       ...

The things you can accomplish with this new capability are virtually limitless.

Python 3.11 Will Include A TOML Parser 

TOML parser in the Python standard library has been approved by the Python Language Steering Council. As a result, you will be able to install tomllib and have a component that can parse TOML files in Python 3.11

Tom’s Obvious, Minimal Language is referred to as TOML. Similar to how JSON, XML, and YAML files organize data, it is a means to organize data in a text file. The TOML standard is constructed in a way that makes it simple for anyone to create TOML files for usage as configuration files in text editors. A sample TOML file is shown below:

# My Dungeons & Dragons character.
name = "Mordnilap"
class = "Magic user"
maxHP = 14
currentHP = 2
inventory = ["apple", "enchanted staff", "healing potion"]
cursed = false
[spells]
# The integers represent how many times a day they can be cast.
fireball = 3
featherFall = 2

You may parse this text and create the following Python dictionary using the TOML parser that will be included in Python 3.11: 

{'class': 'Magic user',
'currentHP': 2,
'cursed': False,
'inventory': ['apple', 'enchanted staff', 'healing potion'],
'maxHP': 14,
'name': 'Mordnilap',
'spells': {'featherFall': 2, 'fireball': 3}}

It already has XML and JSON parsing modules (the xml and json modules, respectively) in its standard library. The Python 3.11 standard library will include a module for parsing TOML files, which means that all Python programs will be able to access it without having to install a third-party module.

TOML files aren’t Python-specific. TOML is just a text file format, which can be read by programs written in any programming language. It should come as no surprise that the Python standard library will include a TOML parser, as many tools in the Python ecosystem use this format.

Conclusion

Additionally, Python became 22% quicker overall in this release. By the time the final release comes out in approximately October, it might even get faster!

Additionally, Python 3.12 is already in development. Stay tuned with us if you want to be aware of all the most recent language developments.

hire python developer

 

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa