You've successfully subscribed to Better Data Science
Great! Next, complete checkout for full access to Better Data Science
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.

Top 3 Radical New Features in Python 3.11 - Prepare Yourself

Top 3 Radical New Features in Python 3.11 - Prepare Yourself

Python 3.11 is not out yet, but you can take the RC version for a spin. Spoiler alert: It's amazing

Python 3.11 is expected to air in October 2022. That's five months away, so what's with the hype? As usual, we can test the RC version, and see how it compares with Python 3.10.

That's exactly what I've done. I've set up two Docker containers, one for Python 3.10 and the other for Python 3.11, and attached them to separate Visual Studio Code windows. We'll run exactly the same code in both, and see what the new release brings.

TL;DR - There are a lot of new features, but I've decided to write about three. These are better error locations, Exception notes, and a built-in TOML parser. It's quite an improvement over the older Python releases. Reference the official changelog for a full list of new features.

Did you know Python 3.11 will be up to 60% faster than Python 3.10? Here's a detailed benchmark.

Don't feel like reading? Watch my video instead:


Python 3.11 Feature #1 - Enhanced Error Locations

When printing tracebacks, the Python interpreter will now point to the exact expression that caused the error instead of just the line. For example, here's a code snippet that will throw an error since the list item at the given index doesn't exist:

if __name__ == "__main__":
    l = [1, 2, 3]
    print(l[3])

In Python 3.10, you'll get the following output:

Image 1 - Enhanced error locations (1) (image by author)
Image 1 - Enhanced error locations (1) (image by author)

On the other hand, Python 3.11 will print the following:

Image 2 - Enhanced error locations (2) (image by author)
Image 2 - Enhanced error locations (2) (image by author)

Python 3.11 underlines the exact part of the code that caused the error, which is neat. The story is similar with, let's say, Python dictionaries. The following code snippet will raise an error because the dictionary doesn't have that key:

if __name__ == "__main__":
    d = {"key1": "value1", "key2": "value2"}
    print(d["key3"])

Here's what Python 3.10 spits out:

Image 3 - Enhanced error locations (3) (image by author)
Image 3 - Enhanced error locations (3) (image by author)

And here's the output for Python 3.11:

Image 4 - Enhanced error locations (4) (image by author)
Image 4 - Enhanced error locations (4) (image by author)

Once again, Python 3.11 underlines the exact part of the code that caused the error.


Python 3.11 Feature #2 - Exception Notes

Python's Exception class will have a __note__ attribute in Python 3.11. It's None by default, but you can override it with any string you want. Sure, this isn't the most groundbreaking feature, but a note here and there can come in handy if you have dozens of custom exception classes.

Here's the code we'll run in both containers:

class CustomException(Exception):
    __note__ = "Note about my custom exception"


if __name__ == "__main__":
    raise CustomException()

And here's the output for Python 3.10:

Image 5 - Raising a custom Exception (1) (image by author)
Image 5 - Raising a custom Exception (1) (image by author)

As you would expect, Python 3.10's Exception class isn't aware of the new attribute. It's a whole different story with Python 3.11:

Image 6 - Raising a custom Exception (2) (image by author)
Image 6 - Raising a custom Exception (2) (image by author)

Once again, Python 3.11 underlines the exact line which caused an error - an obvious one in this case - but it also prints our note at the end.


Python 3.11 Feature #3 - Built-in TOML Parser

TOML, or Tom's Obvious Minimal Language, is a minimal configuration file format. Before Python 3.11, there was no built-in library to work with TOML configuration files. That changes now.

Below is the code snippet we'll run in both containers:

import tomllib 

DOC = """
[main]
python_version = 3.11

[api]
endpoint_users = "/api/v2/users"
endpoint_posts = "/api/v2/posts"
"""

if __name__ == "__main__":
    doc_parsed = tomllib.loads(DOC)
    print(doc_parsed)

Python 3.10 doesn't have the tomllib library, so the exception is raised instantly:

Image 7 - TOML configuration file (1) (image by author)
Image 7 - TOML configuration file (1) (image by author)

Python 3.11 supports it as has no trouble parsing the TOML string:

Image 8 - TOML configuration file (2) (image by author)
Image 8 - TOML configuration file (2) (image by author)

Installing a dedicated library for parsing TOML files takes seconds, sure, but it's nice to see it will be a built-in functionality from the new release.


Summary of New Python 3.11 Features

We've covered three features coming in Python 3.11. There are others, sure, such as the self type, exception groups, async comprehensions, and more, but you can reference the official changelog if you're interested.

Probably the biggest improvement coming with the new Python version is the speed increase. On average, Python 3.11 will be 15% faster than Python 3.10, and the difference goes as high as 64%. I've compared the two in my detailed benchmark article, so make sure to check it out.

What's your favorite Python 3.11 feature? Please let me know in the comment section below.

Stay connected