Python 3.11
The latest version of Python has been released on 24th October 2022 last week. The 3.11 changelog consist of a lot of bug fixes, improvements, and additional features. In summary, these are the changes.
1. Speed Improvement
Python has a reputation as a slow language. This drawback is countered in several ways. Often programmer productivity is more important than code execution time.
The Python docs claim 3.11 can be up to 60% faster in some instances. Here’s how to perform the benchmark test yourself in order to test speed improvements for yourself.
While the performance increases are noticeable on paper, the specific impact of the improvements will vary from person to person and the stable, final version of Python 3.11 is yet to be released.
2. Better Error Messages
Traceback (most recent call last):
File "/home/realpython/inverse.py", line 6, in <module>
print(inverse(0))
^^^^^^^^^^
File "/home/realpython/inverse.py", line 4, in inverse
return 1 / number
~~^~~~~~~~
ZeroDivisionError: division by zero
Note the ^ and ~ symbols embedded within the traceback. They’re used to guide your attention to the code that’s causing the error. As usual with tracebacks, you should start at the bottom and work your way up.
Getting this extra help in spotting mistakes is useful. However, the annotated tracebacks are even more powerful if your code is more complex. They may be able to convey information that you couldn’t get from the traceback by itself before.
3. CPython optimization
CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language. In version 3.11, the CPython interpreter is much more optimized and much faster than in version 3.10. CPython 3.11 is on average 1.22x faster than CPython 3.10 when measured with the performance benchmark suite, and compiled with GCC on Ubuntu Linux.
4. Self
In this version, there is a Self
Type, which is part of the Python 3 feature that allows users to annotate functions. Function Annotation is a feature that allows you to add metadata to function parameters and return values [4]. This way you can specify the input type of the function parameters and the return type of the value the function returns. For example
from typing import TypeVar
TSchool = TypeVar('TSchool', bound='School')
class School:
def get_school(self: TSchool) -> TSchool:
return self
in 3.11, you can do it like this instead
from typing import Self
class School:
def get_school(self) -> Self:
return self
Should you upgrade it?
Quick answer, not yet. Given that it takes work to upgrade—some additional testing, some tweaks to your code—it can be tempting to put off upgrading Python versions indefinitely. Why worry about incompatibilities, new versions, and whatnot, when you can just stick with your current version indefinitely?
The problem is that Python isn’t supported indefinitely, nor do libraries support all Python versions indefinitely; eventually you’ll stop getting security updates and bug fixes. Python 3.6 ended security updates in December 2021. It’s true, Linux distributions that ship it, like RHEL 8, will continue to provide security updates. But RHEL 8 also packages Python 3.8 and 3.9; you really shouldn’t be using 3.6 anymore. And Python 3.7 will end security updates in June 2023.
So sooner or later you will have to upgrade. And if you’re running on a 5-year-old version of Python, switching becomes a Big Deal—you’ll often end up dealing with more significant cross-version changes in both Python and in libraries at the same time. Which makes upgrading scary.
Instead of one massive scary upgrade event every few years, it’s much safer to have a continuous, ongoing process of smaller upgrades. Whenever a new major Python version comes out, or a new major library version, wait a bit and then switch.
This is to say that at the moment, you really should be using 3.10 if you can. If you’re far enough behind, do a series of upgrades: 3.8, 3.9, 3.10, and eventually, 3.11.
Leave a comment