Python 3.12 focuses on performance, clear syntax and more precise error messages
The Python Software Foundation has released the first beta of Python 3.12. In the fresh version, the programming language comes with some syntactical additions, including for generic classes, functions and type aliases as well as for F-strings. There are also adjustments aimed at better performance and improvements in error messages.
With the first beta, Python 3.12 is considered feature complete, so new functions are no longer to be expected. The release roadmap for Python 3.12 includes three more betas and two release candidates before the final release is scheduled for October 2nd.
Python has known formatted string literals or F-strings since version 3.6. They allow you to easily integrate expressions into strings and thus include variables directly, for example. However, the original Python Enhancement Proposal PEP 498 dispensed with formal syntactical specifications and has some limitations. For example, a string delimited with single quotes (‘) does not allow (‘) within the expressions. The PEP 701 included in Python 3.12 now formalizes the syntax for F-strings and thus expands the possible uses.
Python 3.12 also offers a new, clearer syntax for generic type aliases, functions and classes, which is based on other programming languages. The associated PEP 695 compares the old and new notation for generic classes:
# bisherige Schreibweise from typing import Generic, TypeVar _T_co = TypeVar("_T_co", covariant=True, bound=str) class ClassA(Generic[_T_co]): def method1(self) -> _T_co: ... # ab Python 3.12 class ClassA[T: str]: def method1(self) -> T: ...
performance and error messages
The additions intended to improve performance include PEP 709, which implements comprehensions, i.e. filtered copies of lists, dictionaries and sets, differently internally. Until now, Python has used nested functions for this. From version 3.12 it uses inlining instead. The individual comprehensions should ideally work twice as fast, and the proposal speaks of an 11 percent improvement in performance of a real application with many comprehensions.
Meaningful error messages were already the focus in version 3.11. At that time, PEP 657 ensured that the Python interpreter displayed the exact expression in which an error occurred in tracebacks. Previously, it had merely spat out the associated line number. In Python 3.12, error messages indicate, among other things, potentially missing module import statements as the cause of a NameError:
sys.version_info Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'sys' is not defined. Did you forget to import 'sys'?
There are also indications that assignments may have been mixed up if the code says import foo from bar instead of from bar import foo.
Less GIL restriction
The Python community has been concerned with an innovation for a long time: a way out of the trap of the Global Interpreter Lock (GIL). It restricts concurrent programs because all Python interpreters are subject to the same global lock. PEP 684 allows to create sub-interpreters that have independent locks.
The related and five-year-old PEP 554 for multiple interpreters in the standard library via an interpreters module, on the other hand, is only marked for launch in the upcoming version 3.13.
Another noteworthy new feature in Python 3.12 is that the perf profiler on Linux now outputs the names of Python functions in traces. In addition, numerous features marked as deprecated are no longer available in the current release.
More details can be found in the blog post on the first beta. The full list of changes can be found in the changelog. (rm)
To home page