Python 3.11 new features

In this section we are going to discuss the new features of the Python 3.11 programming languages. Top feature of Python 3.11 is the performance enhancements up-to 60% as compare to Python 3.10.

Python 3.11 new features

Python 3.11 new features - What are the new features of Python 3.11

Python 3.11 is the new release of the Python 3.x programming language. In this section we are exploring the new features and enhancements of Python 3.11 programming language. Python 3.11 is released October 28, 2022 and it comes with many new advanced features. Python 3.11 is released but it's still not a production version, but developers can use this version of Python to test their applications and make changes to their applications if it does not work with the Python 3.11. This will help them in preparing their application for Python 3.11 one production version is released.

Developers can also use the non-production version of Python 3.11 to try the new features released in the version. Also compare the performance of their application on Python 3.11. We are excited about presenting the new feature of Python 3.11.

Python 3.11 is more powerful and comes with the features like faster interpreter, intelligible errors, powerful hints and many tweaks to provide better performance in the production. In this section we are going to explore the new features, tweaks and enhancements of Python 3.11.

Python 3.11 is much faster than Python 3.10 - Faster CPython Project

CPython 3.11 is much faster than CPython 3.10. According to the company it is on average 25% faster than CPython 3.10. This performance was measured with the performance benchmark suite, and compiled with GCC on the Ubuntu Linux operating system. Based on the project code and the workload, CPython could run your code up to 10-60% faster. The CPython project focuses on the two major areas in Python namely faster startup and faster runtime. Apart from this there are many other optimizations that are done under this project.

The Faster CPython Project is all about making Python faster by making it run faster at startup and at runtime. Python 3.11 brings performance improvements, developers are encouraged to migrate and test their code on Python 3.11 to take full advantage of faster Python 3.11.

Python 3.11 brings Fine-Grained Error Locations in Tracebacks

Under PEP 657, Python 3.11 brings Fine-Grained Error Locations in Tracebacks in this release of the software. This feature will display the line number and the actual part of the code in the line causing exception while code execution. All this information will be available as part of the error log which will help the developer in finding out the exact part of the code responsible for the error. This will further help developers in debugging the code in a much better way. This is one of most welcomed features of Python 3.11, which makes code debugging much easier for the developers.

Following screenshot gives an idea how the error message looks like:

Python Tracebacks

PEP 654 - Exception Groups and except*

Python 3.11 brings Exception groups and except that allow programs to raise and handle multiple unrelated exceptions simultaneously. This version of Python introduced a new standard exception type, the ExceptionGroup. These are powerful and represent a group of unrelated exceptions, which can be propagated together. It also brings a new syntax except* for handling ExceptionGroups, which makes exception handling in a much better way.

PEP 680 - tomllib: Added the support for tomllib

Python 3.11 added tombllib which can be used for parsing TOML in the Standard Library. So, developers using TOML in their application will be benefited with this feature.

gh-90908 - Introduce task groups to asyncio

Asyncio is very popular among other programming languages and now in Python 3.11 task groups are introduced to asyncio, which makes Python programming feature rich.

gh-34627 - Python 3.11 brings Atomic grouping ((?>...)) and possessive quantifiers (*+, ++, ?+, {m,n}+).

Python 3.11 brings Atomic grouping ((?>...)) and possessive quantifiers (*+, ++, ?+, {m,n}+); and they are supported in the regular expressions. This is also a good improvement in the Python programming language.

PEP 673 - Self Type

Under the PEP 673, Python 3.11 brings a simple and intuitive way to annotate methods, which will return the instance of their class. This is similar to the PEP 484 that uses TypeVar-based approach, but it is more concise and easier to follow.

Example of Self Type in Python 3.11:

class Shape:
def set_scale(self, scale: float) -> Shape:
self.scale = scale
return self

Shape().set_scale(0.5) # => Shape

PEP 646 - Variadic Generics

Through PEP 484, Python 3.11 introduced TypeVar, which is used for creation of generics parameterised with a single type. The TypeVarTuple is a variadic type variable, enabling variadic generics. There is a wide variety of uses of Variadic Generics. This can be used as the type of array-like structures in numerical computing libraries such as NumPy and TensorFlow. This can also be parameterised with the array shape, which further enables the static type checkers. The static type check can be used to catch shape-related bugs in code. So, this is another welcoming feature of Python 3.11.

PEP 675 - Arbitrary Literal String Type

In this version of Python a new supertype of literal string types: LiteralString is introduced. This can be used in Python 3.11?s functions for accepting the arbitrary literal string types, such as Literal[?foo"] or Literal["bar"].

PEP 655 - Marking individual TypedDict items as required or potentially-missing

TypeDict was introduced in Python 3.11 which can be used to make a structured dictionary that has specific keys and the values of a specific type. So, you can now use the TypedDict to make a dictionary with predefined types of keys and values.

PEP 681 - Data Class Transforms

Python 3.11 introduced the Data Class Transforms. In the Dataclass transforms the typing.dataclass_transform decorator is added. This will provide an indication as to how a given function, class, or metaclass behaves like a dataclass.

Python 3.7 dataclasses are used for classes that follow the common pattern of creating properties based on the initialization parameter. But there was no standard mechanism for allowing this behaviour. Now Python 3.11 is now much standardised.

Related Python Tutorials: