Best Features in Python 3.10

Python 3.10 was released with some amazing features such as better error messages for easier debugging and finally the match-case statements.

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 in python
erroneous code
vague error in python 3.8
Not a very easy-to-understand error before python 3.10
specific error in python 3.10
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!

<img src="https://miro.medium.com/max/700/1*yN5TJg-f1kCAxHbBNEuKQA.jpeg" alt="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
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 statements in Python 3.10
Match-Case Statement
Result with Target = 24
Result with Target = 24
Result with Target = 23
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
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 in Python 3.10

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 in python 3.10
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 in python 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.

Total
0
Shares
Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Prev
Understanding and Implementing Logistic Regression Algorithm (Part 2)| Python | Machine Learning

Understanding and Implementing Logistic Regression Algorithm (Part 2)| Python | Machine Learning

THIS IS A TWO-PART SERIES

Next
SweetViz: Easy EDA and applied Data Science with Python

SweetViz: Easy EDA and applied Data Science with Python

EDA stands for ‘Exploratory Data Analysis’

You May Also Like