What’s New in Python 3.10?
Python 3.10, the latest release of Python is in its beta phase. This article explores the major changes and updates to the previous version.
Python is a very versatile, flexible, and interactive programming language. The application of Python spreads across various fields of the tech industry, most notably, web development, machine learning, artificial intelligence, and data science.
Python is a straightforward language that requires less time and resources to learn, making it widely accessible compared to other high-level languages like Java or C++.
Recently, Python contributors switched to a yearly update cycle to introduce new features and improve the existing ones. Python 3.10 is the latest release of Python and we’ll look at some of the most important updates in this article.
Structural Pattern Matching is the most anticipated feature of Python 3.10. This feature introduces a switch/case-like feature as we have in other programming languages to Python. In addition to the switch-like feature, Python 3.10 Structural Pattern Matching brings some extra features, making it even more powerful than the usual switch conditional statement.
Python uses the
match keyword instead of
switch with the
case keyword defining each different scenario. Below is an example of the usage;
x = 3 + 4 match x: case 9: return "That's too high..." case 5: return "You're two steps away ..." case 6 | 8: # Combining multiple literals with `|` return "Oops! That was so close!" case 9: return "Too small..." case _: return "That's too random!"
The example above is the basic usage of Python 3.10’s
match/case conditional statement. Like we have in “case 6”, Python allows you to compare multiple values in the same case statement using the or
Python 3.10 also allows you to use wildcards (
_) as we have in the last
case statement that acts as a default value, similar to what we have in other programming languages.
One of the extra features that make Python 3.10’s
match/case conditional statement more powerful than the switch statement in other programming languages is the ability to match complex patterns. Here is an example:
def http_error(status): match status: case 400: return "Bad request" case 404: return "Page not found" case _: return "Opps! I dont understand that" http_error(500) # Opps! I dont understand that
We used Python tuple to match complex parameters against each other using the match statement. As we used in the first example, the wildcard (
_) is also used to stand in for default value.
Another feature of Python 3.10 Structural Pattern Matching is Guards.
Guards enable you to follow the top pattern with a Guard, an
if statement, which evaluates the subsequent
case statement if the Guard evaluates to true. Below is an example:
match input: case [x, y] if x > MAX_INT and y > MAX_INT: print("Got a pair of large numbers") case x if x > MAX_INT: print("Got a large number") case [x, y] if x == y: print("Got equal items") case _: print("Not an outstanding input")
Other Python 3.10 pattern matching features include classes, literals and variables, and nested patterns, allowing you to create complex conditional statements.
Precise Error Messages
In previous versions of Python, debugging a code is pretty difficult due to poor error messages. Error messages generated by the Parser from previous versions of Python are not specific.
Consider the code below:
print("Hello Reader" print("Welcome to The Chief IO") #The Error Message File "<string>", line 2 print("Welcome to The Chief IO") ^ SyntaxError: invalid syntax
The error message generated points that the error is in line 2, whereas the actual error is in line 1. This makes debugging hard.
Python 3.10 proposes a more accurate and line-precise error message, assisting you in figuring out where the problem is from.
For the same example above, Python 3.10 will generate the following error message which is clearer;
File "<string>", line 1 print("Hello Reader") ^ SyntaxError: ‘(’ was never closed.
A new method,
bit_count, is introduced in Python 3.10. This method will return the number of ones present in the binary representation of an integer.
This method is also known as Population Count (popcount) and it gives you a faster way of counting non-zero bits in an integer.
New Type Union Operator
Python 3.10 also comes with an improved type union checking syntax that will help you write cleaner codes. Instead of using
typing.Union to compare two data types, python 3.10 enables you to use the or (
|) operator with type annotations and also functions like
Here is an example of the old syntax;
def func(value: Union[int, float]) -> Union[int, float]: return value
With the new | operator, we can write the same code like this;
def func(value: int | float) -> int | float: return value
Here is an example of the usage with isinstance() function;
isinstance("hello", int | str) # True
Deprecation and Removal of Some Backward Compatibilities
Some earliest support for abstract base classes and old import semantics will be dropped from Python 3.10 onwards.
Some of the deprecated syntax includes
Distutils is also discontinued and replaced with
A new optional Keyword argument,
strict is added to the
zip function in Python 3.10. If you set the strict keyword to
true, the iterables that you are zipping must be of equal lengths, or else, an error value will be raised.
Get similar stories in your inbox weekly, for free
Share this story:
In this blog post, we’ll help you ensure that your backup systems will perform as …