One of the most popular programming languages of recent times has just gained a version with several important features. I speak of Python 3.11. Among its new attributes is a performance boost of up to 60%. Another is to display more objective error messages, which clearly point out the problematic expression.
The latest Tiobe index list places Python as the most popular programming language at the moment, ahead of C, Java, C++ and C#. Therefore, improving the experience of developers working with Python is, above all, a matter of commitment to the community.
via Twitter, Pablo Galindo Salgadoa member of the language council, made it clear that this commitment is taken seriously:
On the CPython release team, we put a lot of effort into making 3.11 the best possible version of Python. Improved traces, faster Python, expect* and exception groups, typing advances, and more.
Python up to 60% faster
Let’s start with the most relevant news. Relative to version 3.10, Python 3.11 is between 10% and 60% faster in working with certain instances. In others, there may be no noticeable performance gain. But generally speaking, the developer who works regularly with the language is to be expected to notice the difference.
Speaking specifically of CPython (the main implementation of the language), the average performance gain is 25% compared to the previous version. This result was achieved in pyperformance (benchmark tool) with CPython 3.11 compiled via GCC on Ubuntu Linux.
There are several factors that contribute to this. For example, function calls now require less memory. Also, the Python interpreter now boots faster — somewhere between 10% and 15% faster than version 3.10.
Clearer error messages
Error messages are displayed to alert you that something is obviously wrong. But, not infrequently, the developer has to spend neurons to find out where, exactly, the fault is.
Perhaps this problem still exists in Python 3.11 in some form, but on a much smaller scale. That’s because the new version brings error messages designed to pinpoint the location of the error precisely.
On Twitter, the developer Ned Batchelder shows how it makes a difference. Notice that, in the second code of the example given by him, in Python 3.11, the characters ~ and ^ are used to indicate which expression is the fault. In the first, in Python 3.10, the error message is not accompanied by this indication:
Here’s another simple example, this one taken from the Python 3.11 news page:
Traceback (most recent call last): File "calculation.py", line 54, in <module> result = (x / y / z) * (a / b / c) ~~~~~~^~~ ZeroDivisionError: division by zero
Performance gains and more straightforward error messages are the main attributes of Python 3.11, but not the only ones. Among the others are:
- handling of exception groups through the new except* syntax and the new ExceptionGroup exception type;
- inclusion of the tomllib module so that the language can read or analyze files in TOML (Tom’s Obvious Minimal Language) format;
- removal of “dead batteries”, ie obsolete modules from the Python standard library;
- CPython now has native support for WebAssembly, albeit experimental.
All details can be found in the official Python 3.11 What’s New List. The address for downloading the language is this.