Join us

What’s New in Python 3.10?

1_XYJgyus7oBqw5jLkwJlORQ.png

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.

All versions of Python below 3.6 are obsolete.

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;

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 | operator.

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:

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:

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:

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;

Population Count

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 isinstance() and issubclass().

Here is an example of the old syntax;

With the new | operator, we can write the same code like this;

Here is an example of the usage with isinstance() function;

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 find_loader()/find_module, load_module and _load_attribute.

Support for Distutils is also discontinued and replaced with setuptools and packaging.

Strict Zipping

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.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

The Chief I/O

Insights for Cloud Native Leaders

Avatar

Aymen El Amri

Founder, FAUN

@eon01
Founder of FAUN, author, maker, trainer, and polymath software engineer (DevOps, CloudNative, CloudComputing, Python, NLP)
User Popularity
2k

Influence

208k

Total Hits

38

Posts

Mentioned tools