Early bird prices are coming to an end soon... ⏰ Grab your tickets before January 17

This article was published on July 2, 2021

How to use the coolest new features in Python 3.10

A rundown of all the coolest features in Python 3.10


How to use the coolest new features in Python 3.10

Python 3.10 development has stabilized and we can finally test out all of the new features that will be included in the final release.

We’ll cover some of the most interesting additions to Python — structural pattern matching, parenthesized context managers, more typing, and the new and improved error messages.

Check out the video version of the article here:

Structural pattern matching

Structural pattern matching is an incredible feature to be added to Python — truly awesome.

Imagine an if-else statement that looks like this:

You take that and you modify the syntax so it looks more like this:

That is the new match-case statement — cool, but nothing special so far.

What makes the match-case statement so interesting is something called structural pattern matching.

Structural pattern matching allows us to perform the same match-case logic, but based on whether the structure of our comparison object matches a given pattern.

So let us define two dictionaries, both with different structures.

Now, we could write a pattern to match dict_a like so:

And a pattern to match dict_b too:

If we put both of these together in a match-case statement, alongside what is effectively an else/catch-all with case _ — we get:

Pretty cool right? I’ve already found this incredibly useful for data processing — an example of which you can find in this video at the 15:22 mark.

Parenthesized context managers

A smaller change that stems from a much larger change that appeared with Python 3.9 — the new PEG-based parser.

The previous Python parser had many limitations, which restricted the Python devs in which syntax they could allow.

Python 3.9’s PEG-based parser removed these barriers, which long-term could lead to more elegant syntax — our first example of this change is the new parenthesized context managers.

Pre Python 3.9, we could write something like this to open two (or more) file I/O streams:

That first line is pretty long, too long in fact. But due to parser limitations, the only way we could split this line across multiple lines was using the \ line continuation character:

It works, but it’s not Pythonic. With the new parser, we’re now able to split this line across multiple lines using parentheses like so:

Which is Pythonic.

Now, before we move on — there is one minor oddity in this new feature. It’s not entirely new…

If we write:

In Python 3.9 — it works. That is because the new parser enabled this syntax, despite it not being officially supported until Python 3.10.

More typing

There are more updates to Python’s typing features too, which I’ve written about in more detail here if you’re interested.

Easily the most interesting addition here is the inclusion of a new operator which behaves like an OR logic for types, something which we previously used the Union method for:

Now, we don’t need to write from typing import Union, and Union[int, float] has been simplified to int | float — which looks much cleaner:

Better error messages

Tell me you didn’t jump right on over to Google the first time you saw:

SyntaxError: unexpected EOF while parsing

The number one result in Google when entering SyntaxError suggests that many of us certainly did at some point.

syntax
Unexpected EOF while parsing — a *simple and elegant* way of saying we missed a parenthesis

It’s not a clear error message, and Python is full of less than ideal error messages. Fortunately, someone noticed — and many of these messages have been improved significantly.

There are a few more changes that are mentioned on the official change list — but didn’t seem to show during testing, including:

from collections import namedtoplo> AttributeError: module ‘collections’ has no attribute ‘namedtoplo’. Did you mean: namedtuple?

Here, the AttributeError is the same as before, but with an added suggested attribute name — namedtoplo is identified as a potential typo of the attribute namedtuple.

In a similar vein, we see the same improvement for NameError messages:

new_var = 5
print(new_vr)
> NameError: name ‘new_vr’ is not defined. Did you mean: new_var?

There are plenty of other updates to error messages too! Check them all out here

That’s all!

The full release is expected on 4th October 2021, between then and now the Python devs will be working on improving what has been added already — but no new features will be introduced.

If you’d like to check it out yourself, 3.10.0b1 can be downloaded from here.

I hope you enjoyed this article! If you have any questions, let me know via Twitter or in the comments below. If you’d like more content like this, I post on YouTube too.

This article was originally published on Towards Data Science by James Briggs, an AI Consultant based in London. He is fascinated by the phenomenal advances that are being made within the tech ecosystem daily and loves writing about AI, Python, and programming in general. 

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with