Best Features in Python 3.10

Moosa Ali
CodeX
Published in
5 min readOct 8, 2021

--

If your work or study has anything to do with data or artificial intelligence then you must be aware that quite recently the latest version of python was launched i.e. Python 3.10.

This new feature comes with certain features that were much awaited and I’ll be highlighting them in this article. If you are interested in going into the details of this update you can read the detailed documentation here.

Let's get to it.

User-Friendly syntax error messages

meme

Before Python 3.10, the syntax error messages were not exactly very specific and it could be very difficult to debug especially if you have a large chunk of code. Well, that's changed with the new version. The comparisons below explain it perfectly.

erroneous code
erroneous code
vague error
Not a very easy-to-understand error before python 3.10
specific error
Error message with python 3.10
erroneous dictionary
Erroneous dictionary
vague error
Error before python 3.10
specific error
A much more specific error message with python 3.10

Switch-case is finally here!

Photo by <a href=”https://unsplash.com/@hobiindustri?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Hobi industri</a> on <a href=”https://unsplash.com/s/photos/switch-button?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
Photo by Hobi industri on Unsplash

Well, it's not exactly called a switch-case statement but it works in almost the same way.

Python 3.10 introduces the match-case statements or as they call it the ‘Structural Pattern Matching’

*This Structural Pattern Matching deserves its own article but for now I'll stick to just its overview*

The example below shows the syntax for using a Match-Case statement.

Match-Case Statement
Result with Target = 24
Result with Target = 23

Those who have used the Switch-case statement in other languages would be familiar with this syntax. A pattern is passed in front of the ‘match’ keyword and a few possible cases are defined right below it. If the pattern matches any of the cases passed below, that particular block of code is executed as demonstrated by the examples above.

There is also a default case set which is identified by the ‘_’ in the last case statement, this default case is executed when your pattern matches none of the cases defined.

Result with Target = 25

It is also important to note that defining the default case is optional and if you don’t then the ‘Match-Case’ statements will be skipped with no execution (or what we call in programming terms; no-operation).

Match-Case allows for matching of data values, data types, data structures, mathematical expressions, and many more; a very flexible function indeed.

Parenthesized Context Managers

Photo by Andrea Piacquadio from Pexels

A context manager in python is used for the allocation of memory when needed and freeing up that memory when its use is over. The ‘with’ statement in python is one of the most common examples of a context manager and its most common use case is the opening of files to read/write data. An example of this is shown below;

context manager example

Here our file is opened and is recognized by the object variable f1. All the operations we will perform will be using this variable however f1 is only accessible within the indented block. All operations on f1 must be performed within this indented block since once our code leaves the indented block, the file is automatically closed (the purpose of the context manager).

Before Python 3.10 you could only manage context for one object with one ‘with’ statement, the following code would throw a syntax error if executed on python versions less than 3.10.

parenthesized context manager

However, this functionality has been added in Python 3.10 and now the above code would open the above files for writing purposes and perform the execution as coded.

The End

These are 3 of the most notable features introduced in Python 3.10 amongst many other subtle updates, if you are interested you can read the complete update documentation here.

More like This

--

--

Moosa Ali
CodeX
Writer for

Blogger | Data Scientist | Machine Learning Engineer. For more content, visit: www.writersbyte.com. Support me on: ko-fi.com/moosaali9906