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
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.
Switch-case is finally here!
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.
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.
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
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;
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.
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.
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.