New Features in Python 3.10

On the 4th of October, 2021, python 3.10 was launched, and it came with a lot of new features. In this article i will share some of the most important features in python 3.10

Better Error Messages

The newest version of python (3.10), the error messages shown has been significantly improved. If you have been using the older version of python for a while, I am sure that you have noticed the weird messages being thrown by python in response to some errors. For example, when writing python code that requires an opening and a closing parentheses or brackets, if the bracket or parentheses is missing, python locates the particular line of code and tells you exactly what is missing, instead of just showing "SyntaxError: unexpected EOF while parsing".  In the image below, you will notice that on line 14, the list is supposed to be closed with a "}"

when this code is executed with python 3.9 or older, it shows the error message below

In the image above, you will see that it says "SyntaxError: invalid syntax". That error message is correct, but it's not detailed, and it does not point to the line where the real error comes from. But if the code is executed with python 3.10, it shows this message

As you can see in the image above, the error message shows the specific line the error comes from, and it shows what causes the error.

Another feature that was added in the error messages section is name suggestions. Let's take the code in the image below as an example:

You will notice that in the image above, the variable is written as "name" instead of names. If this is executed with the older version of python, it will throw this error

But if it is executed with python 3.10, it will show this error

You can see in the image above that it did not just only say that "'name' is not defined", but it also suggests for the name of variable i was trying to print.

Structural Pattern Matching

Structural pattern matching allows you to match variables with patterns of values. It brings the match/case statement into Python, which is very similar to the switch/case statement. It does a particular task only if it finds a match while taking an object and testing it against one or various match patterns.

The generic syntax of pattern matching will look something like this:

An example of this is shown with the image below:

Note that in the example above, we also use "|" to combine several literals in a single pattern.
To check more about this feature, you can check the official documentation here

Parenthesized Context Managers

Using enclosing parentheses for continuation across multiple lines in context managers is now supported. This allows formatting a long collection of context managers in multiple lines in a similar way as it was previously possible with import statements.
Length-checking in zip()

When we have two lists and we want to join each value together, we use the zip() function. For example, in the image below, you will see that we have two lists, the first list has four elements, and the second list has three elements.

When joined together using the zip() function, you will notice that the last value of the first list is excluded because it has no value to be joined with

But, there is a new feature which will allow the program to automatically check the length of the two lists before trying to combine them together. The image below shows how to do this using the "strict" keyword:



There is a tonne of new feature introduced in python 3.10, only a few is covered in this article. You can check the full documentation here

If you enjoy this article, drop a comment and tell me what you think about it. You can also consider joining my newsletter here

Post a Comment