In this blog, which is a first of a three-part series, we talk about the structural pattern-matching support in Python.
Python 3.10 introduced structural pattern-matching syntax in python. Typically used in functional programming languages such as Haskell, rust and hybrid programming languages such as scala, more and more object-oriented programming languages are adopting spm syntax as a core feature.
We start with basic definitions, types of pattern-matching, and basic pattern matching examples in this blog & move to pattern matching examples in the second part, and finally discuss class pattern in the third part.
Structural pattern matching is a feature in some programming languages that allows you to compare objects based on their structure rather than just their type. This can be useful when you want to compare objects that may have different types but have the same underlying structure.
The specific syntax for structural pattern matching varies depending on the programming language. In some languages, it is implemented using a keyword such as match or a case, while in others it is implemented using the instanceof operator in combination with special syntax for specifying patterns.
Structural pattern matching can make your code more expressive and easier to read, as it allows you to directly compare the structure of objects without having to write long and complex type-checking code. It is available in languages such as Scala, Rust, Java (as of version 14), Python (as of version 3.10)
In python, a pattern is a value defined after the case keyword which acts as a minimal structural constraint against which a subject is matched.
In some cases, it also helps us bind the subject partly or fully to a free variable.
There are 9 different types of patterns
This includes matching literals in python including number, string, None, True, False, etc.
Let us understand with an example
Here is the output in different cases of subject
In the above example, the case for 1.0 or True will not be called even if the value for a subject is 1.0 or True respectively.
Instead, the case for 1 will be used, As for literal pattern matching == or __eq__ is used.
Similarly, 0 will be a match for the subject value False.
F-strings are not allowed to be used as patterns or cases.
These are patterns where we can assign a subject(partially or fully) to a free variable.
Here are the different types of outputs
These patterns use single underscore _ to match a part of or whole subject.
Here are the different outputs
The wildcard pattern can be used when part of the subject matched to the wildcard is not explicitly used as in the case of the capture pattern.
This pattern is used to bind the free variable(name) on the right side of as to the subject matched with the pattern on its left side.
Here are the different outputs
The _(wildcard) cannot be used on the right side of as, as it can’t be reused. In case it’s used on the right side of as it will result in a syntax error. (SyntaxError: cannot use _ as a target).
This pattern works similarly to a or keyword but uses pipe | to do so.
Here are the different outputs
A guard is a if condition on top of patterns.
Here are the different outputs
Its important to know that a guard condition is only checked if an initial pattern is matched.
In this part one of three parts series of structural pattern matching in python, we discussed the definition and basic pattern matching in python 3.10.
In the next blog, we will discuss Value Patterns, Sequence Patterns, and Mapping Patterns.