Londonchiropracter.com

This domain is available to be leased

Menu
Menu

How to use the coolest new features in Python 3.10

Posted on July 2, 2021 by admin

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!

So, these were some of the key new features being introduced with Python 3.10.

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. 

Source

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • LG Electronics and Nvidia are in talks on robotics, AI data centres, and mobility
  • Sequoia is giving away the hardware for an AI project it cannot invest in. That is the point.
  • Trump says Anthropic Pentagon deal is ‘possible’, weeks after blacklisting the company as a national security risk
  • Samsung and IKEA just made the $6 smart home real, and your TV is already the hub
  • OpenAI recruits Cognizant and CGI to take Codex into enterprise software shops worldwide

Recent Comments

    Archives

    • April 2026
    • March 2026
    • February 2026
    • January 2026
    • December 2025
    • September 2025
    • August 2025
    • July 2025
    • June 2025
    • May 2025
    • April 2025
    • March 2025
    • February 2025
    • January 2025
    • December 2024
    • November 2024
    • October 2024
    • September 2024
    • August 2024
    • July 2024
    • June 2024
    • May 2024
    • April 2024
    • March 2024
    • February 2024
    • January 2024
    • December 2023
    • November 2023
    • October 2023
    • September 2023
    • August 2023
    • July 2023
    • June 2023
    • May 2023
    • April 2023
    • March 2023
    • February 2023
    • January 2023
    • December 2022
    • November 2022
    • October 2022
    • September 2022
    • August 2022
    • July 2022
    • June 2022
    • May 2022
    • April 2022
    • March 2022
    • February 2022
    • January 2022
    • December 2021
    • November 2021
    • October 2021
    • September 2021
    • August 2021
    • July 2021
    • June 2021
    • May 2021
    • April 2021
    • March 2021
    • February 2021
    • January 2021
    • December 2020
    • November 2020
    • October 2020

    Categories

    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    ©2026 Londonchiropracter.com | Design: Newspaperly WordPress Theme