What's new in Python 3.10?
Python 3.10 has just been released, which means we can finally see what new features are in store. So let's take a look, shall we?
Structural pattern matching
This is a big one — It's probably the biggest syntax addition to Python since its original 3.0 release in 2008.
What "structural pattern matching" really means is that now we have a way to write conditional statements based on the structure of a piece of data, instead of logical conditions. It's done through a new match keyword, and multiple case clauses:
You might think that this looks very similar to another popular feature in other languages: switch statements. And you're right — it can absolutely be used as a replacement for switch-case, but match does a lot more. Here are a few examples:
Matching elements in a data structure:
Matching properties in an object:
Matching variable number of properties using spread syntax:
You can find more examples in the "What's new" page and in PEP 636.
Better error messages
As friendly as Python is in terms of readability, its error messages can sometimes be pretty hard to understand. Fortunately, Python 3.10 brings huge improvements in the readability of its error messages.
Some of these improved messages are:
New type union operator
A very common use of the typing module is to create union data types, i.e. variables that can access variables of multiple data types. To do this, we needed to import typing.Union, like so:
Since this was a really common use-case, a shorthand has been added for the same functionality: you can now use the | operator to create unions out of data types:
scrict parameter in the zip() function
Python's builtin zip function is rather handy, it takes in a bunch of iterables, and returns 1 value from each of them until any of them is out of values. For example:
The problem with zip was that, if one of the iterables was longer than the other, those end values will never show up in the zip, and you'll have no way of knowing.
We missed out on 4, 5, and 6 from nums without any way of knowing that. To remedy that, the new strict parameter has been introduced, which throws an error if any of the iterables was of a different length:
Deprecation of distutils module
The distutils package was the old way to create distributable Python packages, to upload on PyPI, for example. The package's functionality has been superceded by the third party packages setuptools and packaging. Because of this, it has been deprecated in 3.10 and will be removed in 3.12.
Other improvements
- Added int.bit_count() method, which returns the number of 1 bits in a number's binary representation.
- Other typing module additions: the TypeAlias type, and user defined type guards with TypeGuard.
- Added sys.orig_argv property, which contains the original args passed to the python command.
Summary
We got some really exciting, big updates this year! For a more detailed look into the changes in this release, you can check out the docs.
Also, if you want to keep up with what's going to show up in Python next year, you can follow the developments in Python's mailing lists, and the latest open PEPs. The plans for next year already seem pretty interesting. 👀