Technology

#How to use the coolest new 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
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!