Python 3.10 is released on 4th October 2021, just after the 30th anniversary of the Python programming language. The newer version has brought some exciting features and improvements along with it. In this article, we will briefly discuss some of the new features that have been released with Python 3.10. So, let’s begin without any ado.
Table of Contents
- Structural Pattern Matching
- Strict Argument for Zip Function
- Improved Error Messages
- Parenthesized Context Managers
- Module Improvements and Optimizations
- Other Updates and Deprecations
Structural Pattern Matching
The Python programming language was lacking a control structure such as a switch statement that exists in the other object-oriented programming languages such as C++, C#, or Java. With structural pattern matching, you can implement a control flow that matches multiple values and execute a piece of code based on the matched values.
To implement structural pattern matching you need to use the “match” keyword followed by the variable or constant to match. The “match” statement contains various “case” statements. The case statement whose value matches the value passed to the “match” command is executed. Here is an example:
In the script below you define a variable “grade”. This variable is then passed to the match command. Since the “grade” variable contains the string “X”, the case statements with strings “A”, “B”, and “C” will not execute. Rather, the last case statement with value underscore “_” will execute which is a placeholder value reserved for the values that don’t match any of the case statements.
grade = "X" match grade: case "A": print ("excellent result") case "B": print ("good result") case "C": print ("average result") case _: print ("unknown result")
Furthermore, with the pipe operator, you can specify the OR condition in the “case” statement. Here is an example:
grade = "X" match grade: case "A": print ("excellent result") case "B": print ("good result") case "C" | "X": print ("average result") case _: print ("unknown result")
In the script above, the third case statement contains a pipe operator between the values “C” and “X”, which means that if the value of the “grade” variable in the “match” command is either “C” or “X”, execute this case statement. As a result, you will see the string “average result” printed on the console.
Strict Argument for Zip Function
The “zip” function in Python is used to create a single iterable by aggregating multiple iterables.
Python 3.10 has introduced a “strict” parameter which when set to true forces the zip function to aggregate only the iterables with an equal number of items.
By default, if you loop over the zipped iterable, the loop stops when the length of the shortest iterables in the aggregated iterables is reached. Here is an example:
age = [10, 20, 30] name = ["Nick", "John"] person = zip(age, name) print(list(person))
[(10, 'Nick'), (20, 'John')]
You can see from the output that only the zip function aggregated the first two values from the age and name lists since the name list only contains two items.
If you set the “strict” parameter to True in the zip function, you will see an error in case if the aggregated iterables do not contain the equal number of items. Look at the following example:
age = [10, 20, 30] name = ["Nick", "John"] person = zip(age, name, strict = True) print(list(person))
ValueError: zip() argument 2 is shorter than argument 1
Improved Error Messages
One of the most amazing features of the Python 3.10 version is the improved error messages. The error messages now convey more meaningful and precise information about the error. As a result, code debugging becomes much easier.
Let’s see a simple example that demonstrates the difference between the error messages in Python 3.9 and Python 3.10.
Running the code below in Python 3.9 returns the error shown in the output:
File "<ipython-input-9-2abc05a6a557>", line 3 print("Python") ^ SyntaxError: invalid syntax
The above error message only shows that there is a syntax error. The type of syntax error is not known. You cannot tell from the error message if a parenthesis is missing or a closing double quotation is missing, etc.
If you run the above script in Python 3.10, you will see the following error message:
File "<ipython-input-9-2abc05a6a557>", line 3 print("to" ^ SyntaxError: '(' was never closed
From the above error message, you can precisely see what the error is i.e. the parenthesis was never closed.
Parenthesized Context Managers
Python 3.10 allows you to format a long collection of context managers in multiple lines using parenthesis with the “with” statement. For instance, the script below formats three context managers using a parenthesis that follows the “with” statement:
with ( ctmanager1() as ctm1, ctmanager2() as ctm2, ctmanager3() as ctm3, ): # code here
Module Improvements and Optimizations
Though no new module has been added to Python 3.10, there have been improvements in several existing modules. Similarly, Python 3.10 comes with various optimizations that make the new version faster and lighter.
The following Python modules have been greatly improved:
To see a full list of the improved modules in Python 3.10, check out this link.
In addition to adding functionalities to the aforementioned modules, optimizations have been made to some other modules as well.
For instance, the “bytearray()”, “bytes()”, and the “str()” modules are now faster compared to their counterparts in Python 3.9. For smaller objects, these modules depict a performance improvement of 30 to 40 percent.
Similarly, in Python 3.10, the runpy module needs to import fewer modules compared to Python 3.9. For instance, on Linux, the “python3 -I -m module-name” command imports only 51 modules in the case of Python 3.10 which is 18 fewer than 69 imported in the case of Python 3.9.
Other Updates and Deprecations
Among the other major updates, Python 3.10 requires OpenSSL (secure socket layer) certificate greater than or equal to 1.1.1. The OpenSSL 1.0.2 is no longer supported with Python 3.10. As a result of this update, the mac, SSL, and hashlib modules have been updated in Python 3.10.
Among the deprecated modules, the distutils are the most noticeable. The module will be removed in Python 3.12. The functionalities for the distutils module are mostly replaced by other third-party modules and packages.
Though Python 3.10 did not introduce any new modules, it has introduced a lot of new features, along with improvements and optimizations in the existing Python modules. Structural pattern matching was one of the most desired features for Pythonistas for a long time now and has been added in Python 3.10. In addition, the improved error messages will render debugging a lot easier and can be a great help for beginner-level Python programmers. Finally, the optimization in various modules makes Python 3.10 much more secure, robust, and faster compared to its previous counterparts.