5 New features in Python 3.11 that makes it the coolest new release in 2022 (2024)

Every year around October, a new version of Python is released. Prior to each release, there is a 17-month development life-cycle where the code is developed and tested extensively. In this cycle, there is also an alpha phase, which goes until end of April. This is followed by a beta phase which might extend till September before the candidate release and final pilot in October. The latest version Python 3.11 is set to release in October this year and the alpha phase is currently ongoing and generally goes on until May 2022.

I was going through the documentation of the latest alpha version and found some pretty awesome new fixes and updates which are worth discussing prior to the beta testing. I will also discuss how one can get access to the 3.11 Alpha version, even with existing Python installations.

5 New features in Python 3.11 that makes it the coolest new release in 2022 (1)

In the following section, I will discuss the 5 features which really stood out for me!

With every new version of Python, they have been working on improving the error handling, especially with the messages that are displayed with the traceback. There were a lot of updates to this feature in Python 3.10 itself which made it easier to find errors in the traceback which was printed in the standard output whenever an error was encountered. Python 3.11 is expected to have an even better error locating feature in the tracebacks. This would make it clear which syntax or object had triggered the reported error.

This could be understood better from the example on the websites.

When printing tracebacks, the interpreter will now point to the exact expression that caused the error instead of just the line as can be seen below.

Previous versions of the interpreter would point to just the line which was causing the error, thereby, making it ambiguous which object was None.

These enhanced error messages are all the more helpful when we are dealing with deeply nested dictionary objects and multiple function calls or even arithmetic expressions [1] (see below). Suppose we attempt a division by zero, then the interpreter will tell us exactly where the issue lies.

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 version 3.10. CPython 3.11 is on average 1.22x faster than CPython 3.10 when measured with the pyperformance benchmark suite, and compiled with GCC on Ubuntu Linux. Depending on your workload, the speedup could be up to 10–60% faster. [1]

In Python 3.11, the developers have mostly focused on faster startup and faster runtime as has been stated in the documentation.

Faster Startup [6]

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

Faster Runtime [6]

In Python, frames are created whenever Python calls a user-defined function. This frame holds the information about the function execution. The following are new frame optimizations in version 3.11 to make it faster during runtime:

  • 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 it’s 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 code, no frame objects are created at all. As a result, nearly all Python functions calls have sped up significantly. This is leading to a almost 3–7% speedup in pyperformance.

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 value [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, say we have a class called School which has a function get_school that returns an instance of the class School. Earlier, if we wanted to annotate the function, we would have to use TypeVar which is very verbose.

In version 3.11, we can just use the Self type which seems much more intuitive and easy to code.

Another cool feature introduced in this version is dealing with multiple exceptions. This feature will allow us to raise multiple exceptions simultaneously using the except* keyword and ExceptionGroup class. Here’s an example below:

With this new feature, the possibilities of what you can achieve are endless.

This is the most interesting feature addition for people who switched from JavaScript to Python and were missing the Async and await functionality of JS. Python hasn’t exactly picked it up but the fundamental concept is quite similar and is very powerful.

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

In the above example, even if t1 fails, t2 will be executed as a task before the error handling happens. More detailed explanation can be found in this tweet by @1st1!

5 New features in Python 3.11 that makes it the coolest new release in 2022 (2)

The 7th version 3.11.0a7 is currently the latest alpha version which was released on 17th April, 2022.

The developers release a new alpha version every month to showcase the new features, test them, and get early feedback from the community. The beta testing is set to start around first week of May so this is the final alpha version in this life-cycle.

Here’s how you can install Python 3.11 alpha version in different ways:

Docker Installation

If you already have access to Docker on your system, then you can download the latest version of Python 3.11 by pulling and running the python:3.11-rc-slim Docker image:

docker pull python:3.11-rc-slim

and then you can run it

docker run -it --rm python:3.11-rc-slim

This drops you into a Python 3.11 REPL.

Windows Installation

To install the alpha version on your Windows OS, you can use pyenv-win . First, update your pyenv installation which ensures that you install the latest version of Python and then install Python 3.11.

pyenv update 
pyenv install 3.11.0a7

Once the installation is complete, you can create a virtual environment to experiment with this version.

pyenv local 3.11.0a7
python -m venv env
.\env\Scripts\activate

Ubuntu/MacOS Installation [2]

The way to get Python 3.11 Alpha version for Ubuntu and MacOS is almost the same as Windows, just the commands are a bit different.

$ pyenv update
$ pyenv install 3.11.0a7
$ pyenv virtualenv 3.11.0a7 311_preview
$ pyenv activate 311_preview

Direct Pre-Release Installation

You can also just install the alpha version from one of the versions available on the official Python website. You can select the latest pre-release and scroll down to the Files section at the bottom of the page. Download and install the file corresponding to your system. See Python 3 Installation & Setup Guide for more information. [2]

These were some of the features that I found pretty cool in Python 3.11 but you can review all the changes within every version using the official documentation. There are going to be many great features which make using Python even more intuitive and user-friendly. There are also some deprecations which are worth looking into.

If there is something interesting that I missed, then feel free to add it in the comments!

5 New features in Python 3.11 that makes it the coolest new release in 2022 (2024)
Top Articles
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 5549

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.