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.
The 💜 of EU tech
The latest rumblings from the EU tech scene, a story from our wise ol' founder Boris, and some questionable AI art. It’s free, every week, in your inbox. Sign up now!
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:
SyntaxError: unexpected EOF while parsing
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:
from collections import namedtoplo> AttributeError: module ‘collections’ has no attribute ‘namedtoplo’.Did you mean: namedtuple?
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:
new_var = 5print(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 outhere
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 — butno newfeatures will be introduced.
If you’d like to check it out yourself, 3.10.0b1 can be downloaded fromhere.
I hope you enjoyed this article! If you have any questions, let me know viaTwitteror in the comments below. If you’d like more content like this, I post onYouTubetoo.
Thisarticlewas originally published onTowards Data SciencebyJames 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.
Story byJames Briggs
Get the TNW newsletter
Get the most important tech news in your inbox each week.