Need help with your Python Website? Schedule a Free Consultation with our Experts.
Learn More

What's new in Python 3.10? Know the most useful features!

Better error messages, structural pattern matching, improved type hinting, and parenthesized context managers are the most prominent upgrades in Python 3.10

Posted by Anubhav De on 19 Nov 2021
What's new in Python 3.10? Know the most useful features!

Python 3.10 is out! But the main question is should you upgrade to 3.10, or stick to your existing version? The only way to answer this question is to evaluate all the features, to understand the differences they will make to your coding flow. And, accordingly decide if it will benefit your use-case, or hinder it.

The purpose of your use will also make a difference, for example if you are a student, tech industry beginner, or a veteran at a software firm. There aren’t too many new updates, but the ones that are can make a prominent difference to your coding. Especially the structural pattern matching, and improved error messages.

Let’s take a look at what’s new in Python 3.10

1. Better error messages

Python has upgraded over its generic error messages. In python 3.10 the error messages are quite detailed and specific. The new improved error messages make it very easy to detect coding errors.

In earlier versions, even when you made simple mistakes such as basic syntax errors, Python would point out the line and prompt a generic SyntaxError: invalid syntax.  message. However, in Python 3.10 you will get an extra line in the error message, pointing out the mistake committed and even suggesting plausible corrections.

Better Error Messages - 1

In the above example, we can see the in line 2 we have used the assignment operator = instead of the comparison operator ==

Better Error Messages - 2

Notice how the error message clearly points out that ( was never closed.

Better Error Messages - 3

Here a type conversion was missing. Thus the error message points out that we need to convert an object type in line 5 to string.

So this is not really an entirely new feature, but an improvement over an existing one. This detailed error message works even when you open a Python file of previous versions in Python 3.10. You can visit What’s new in python 3.10, in the official documentation of Python, to check different (and detailed) examples of improved error messages.

2. Structural Pattern Matching

Structural Pattern Matching is one of the prominent new features in the 3.10 release. This new feature can help you replace your if statements with a more declarative and functional, and shorter block of code. The match statement and case statements act much like the Switch Case construct you find in Java and C++

However, Python warns users to compare pattern matching with switch case; pattern matching in Python 3.10 is better compared with pattern matching found in Scala and Elixir. Pattern matching is useful in extracting information from complex data types, applying form-specific actions on data, and branching on the structure of data.

Structural Pattern Matching - 1

This is how you would write the code in previous Python versions.

Structural Pattern Matching - 2

And this is how you can use the Structural Pattern Matching in 3.10 for the same code. The declarative nature of the match and case statements allows you to further combine multiple lines using or to shorten the block of code.

Structural Pattern Matching - 3

You can also add a Guard to your pattern matching with an if statement. A Guard is when you add a condition to a case.

Structural Pattern Matching - 4

Once again the Python documentation points out valid use cases for this new feature.

Structural Pattern Matching - 5

Structural Pattern Matching - 6

Structural Pattern Matching - 7

Structural Pattern Matching - 8

Structural Pattern Matching - 9

3. Improved Type hinting

Python 3.10 introduces a new type union operator. This new feature allows you to use the x | y syntax to express either type X or type Y. The new type union operator in Python 3.10 improves type hinting; makes the code cleaner.

Improved Type Hinting - 1

This is how we would code in previous Python versions

Improved Type Hinting - 2

This is how you can write the type hinting in Python 3.10 with the new x | y syntax; no need to import union.

Python 3.10 has more typing features like Parameter specific variables and Explicit Type Aliases.

4. Parenthesized context managers

Python 3.10 supports enclosing parenthesis for context managers. This feature allows you to continue the context managers with multiple lines. The following image is of the example cited on Python documentation.

Parenthesized Context Managers

What’s more in Python 3.10?

The above-mentioned features are the most useful and prominent upgrades in Python 3.10, but you will find plenty more new inclusions. New features are added to the standard library that allows you to add optional length-checking to Zip. Python 3.10 has also introduced interpreter improvements that help with debugging. And then finally there are new important deprecations, removals or restrictions.

The bottom line is should you upgrade to Python 3.10. Well, it definitely depends on your comfort zone and use-case but, it’s worth noting that the new features make coding more convenient and cleaner.

- Anubhav

Need help with your Python Website? Schedule a Free Consultation with our Experts.
Learn More

Related Services.

Hire ReactJS Developers
Hire Gatsby Developers
Hire NextJS Developers

We support the Open Source community.

Have a Project in mind?