Python 3.11: Top 5 New Features That You Don't Want To Miss


Python is one of the most popular programming languages in the world. On October 24, 2022, Python released the 3.11 version which has some pretty awesome new fixes and updates. ​​Let’s take a dive and look at some of the new things in this version in this article!

Python Introduction

Python is one of the most used scripting languages in data science (DS) and machine learning (ML). According to PYPL, this is the most searched language on Google in 2022, compared to a year ago.

Every year around October, a new version of Python is set to release. Before each release, the development life-cycle will take about 17 months to develop and test the code extensively. Each new version will have many new features to improve and update the previous version’s error handling. 

Python 3.22 development lifecycle
Python development lifecycle

Top 5 New Python 3.11 Features 

In this following section, we’ll discuss 5 features that really stood out in the 3.11 version of Python.

Error message improved

Prior to Python 3.11, when printing trackbacks, the Python interpreter would not point to the correct expression and throw an error. It will point to the line, which still needs to be more helpful.

The figure below describes the program while encountering with TypeError Exception in Python 3.10:

 TypeError Exception in Python 3.10

As you can see, the message doesn’t tell you much about which part of your data structure happens to be None. In fact, the field “name” is None, therefore you were not able to get item “first” from it.

Meanwhile, Python 3.11 bring out the same error message but is more straightforward:

 TypeError Exception in Python 3.11

You still get the same KeyError because of a missing last field. But in this case, the marker points out intuitively where is the incorrect part, so you can immediately see that the “last” item is an expected field nested inside the name.

So in this latest version, Python has handled this error. It has a better error-locating feature in the tracebacks. This feature will point to the exact expression that caused the error instead of just the line as in the previous versions.

Cpython Optimizations

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, and the speedup could be up to 10-60% faster. The speed boost focuses on two main areas: faster startup and faster runtime.

Faster startup

In this version, Python caches the bytecode in the __pycache__ directory to speed up the process of loading modules. Interpreter startup is 10–15% faster in Python 3.11. This has a big impact on short-running programs using Python.

Faster Runtime

The Runtime in Python 3.11 is faster than in the previous versions, thanks to the following new frame optimizations:

– Streamlined the frame creation process to make it faster.

– Avoided memory allocation by generously re-using frame space on the C stack.

– Reduced the amount of information held by a frame by streamlining its internal frame struct. Previously, frames held extra debugging and memory management information. Old-style frame objects are now created only when required by debuggers.

– For most user codes, no frame objects are created at all. As a result, nearly all Python function calls have sped up significantly. This is leading to an almost 3–7% speedup in pyperformance.

Code annotation with “Self type”

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. This way you can specify the input type of the function parameters and the return type of the value the function returns.

Exception Groups and except*

This document in Python 3.11 proposes language extensions that allow programs to raise and handle multiple unrelated exceptions simultaneously:

– A new standard exception type, the ExceptionGroup, represents a group of unrelated exceptions being propagated together.

– A new syntax except* for handling ExceptionGroup.

AsyncIO Task Groups

For those who switched from JavaScript to Python and await the functionality of JS, this is the most exciting feature addition. 

The idea of TaskGroup is to run nested tasks and continue running them even if one fails. Errors are raised using exception groups, so no error will pass silently. In fact, the previously mentioned ExceptionGroups were needed to implement the TaskGroup feature.

Final thought 

In this article, we have gone through only the most interesting new features in Python 3.11. . There are others, such as the Variadic generics, TOMLLIB, and more, you can reference the official document if you’re interested.

As the trusted IT company for worldwide customers, BHSoft team members always update and master the latest technology to provide the best services to our clients. If you have an idea in mind about a Python project, don’t hesitate to share it with us via e-mail: hello@bachasoftware@gmail.com, or Contact Us to learn more about how we can help you.

Looking for an IT partner?