How to use the coolest new features in Python 3.10
A rundown of all the coolest features in Python 3.10
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:
Thatis the newmatch-casestatement — cool, but nothing special so far.
What makes thematch-casestatement so interesting is something calledstructural 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 matchdict_alike so:
And a pattern to matchdict_btoo:
If we put both of these together in a match-case statement, alongside what is effectively anelse/catch-all withcase _— 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 usingparentheseslike so:
WhichisPythonic.
Now, before we move on — there is one minor oddity in thisnewfeature.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 beingofficiallysupported until Python 3.10.
More typing
There are more updates to Python’s typing features too, which I’ve written about in more detailhereif you’re interested.
Easily the most interesting addition here is the inclusion of a new operator which behaves like anORlogic for types, something which we previously used theUnionmethod for:
Now, we don’t need to writefrom typing import Union, andUnion[int, float]has been simplified toint | float— which looks much cleaner:
Better error messages
Tell me you didn’t jump right on over to Google the first time you saw:
The number one result in Google when enteringSyntaxErrorsuggests that many of us certainly did at some point.
It’s not a clear error message, and Python is full ofless than idealerror 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:
Here, theAttributeErroris the same as before, but with an addedsuggestedattributename —namedtoplois identified as a potential typo of the attributenamedtuple.
In a similar vein, we see the same improvement forNameErrormessages:
That’s all!
Story byJames Briggs
Get the TNW newsletter
Get the most important tech news in your inbox each week.