A look into the pre-release in Python 3.10
Python 3.10 is around the corner and if you want to download the pre-release version of Python 3.10 then head to the link or check out the references at the end of the article.
So, there are few new things added to Python, and if you want to check all of them out head to this link. But here are the two features that I really like:
Structural Pattern Matching
The match statement first evaluates the subject expression. If a comma is present a tuple is constructed using the standard rules. This has a similar structure as the switch case.
This looks like a powerful tool when matching multiple values in an object or even a tuple.

Let’s see a small example wherein you might need to retrieve 2 or 3 values based on input. The match pattern helps you to do that easily.

Let’s create a function that will return Point3d or Point2d object based on input passed as arguments.

The match pattern works with any value thus making this work for strings as well thanks to Python’s dynamic nature.


Allow writing union types as X | Y
A new type of union operator was introduced which enables the syntax X | Y. This provides a cleaner way of expressing ‘either type X or type Y’ instead of using typing.Union, especially in type hints.

References:
Release Date: Aug. 2, 2021, This release, 3.10.0rc1, is the penultimate release preview. Entering the release candidate…www.python.org
This PEP provides the technical specification for the match statement. It replaces PEP 622, which is hereby split in…www.python.org
This PEP proposes overloading the | operator on types to allow writing Union[X, Y] as X | Y and allows it to appear in…www.python.org
Comments
Post a Comment