How to Build a Food...
December 18, 2024
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.
The complete list of enhancements included in Python 3.11 is as follows:
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.
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 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.
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.
Complex arithmetic expressions exhibit the same precision.
Specifically for data scientists who have trouble debugging scripts, the revised error messages are a wonderful improvement.
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
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.
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.
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.
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.