DEV Community

Cover image for New Pattern Matching Feature in Python

New Pattern Matching Feature in Python

James Briggs
418 bio is a teapot
・1 min read

The new structural pattern matching feature arriving to Python 3.10!

Python 3.10 is beginning to fill-out with plenty of fascinating new features. One of those, in particular, caught my attention - structural pattern matching - or as most of us will know it, switch/case statements.

Switch-statements have been absent from Python despite being a common feature of most languages. Python is leapfrogging ahead of those languages by introducing the match-case statement as a switch-case v2.0.

Back in 2006, PEP 3103 was raised, recommending the implementation of a switch-case statement. However, after a poll at PyCon 2007 received no support for the feature, the Python devs dropped it.

Fast-forward to 2020, and Guido van Rossum, the creator of Python, committed the first documentation showing the new match-statements, which have been named Structural Pattern Matching, as found in PEP 634.

Let's take a look at how this new logic works.

Medium Article:

Friend Link (free access):

Discussion (1)

hanpari profile image
Pavel Morava

The only and the most important thing I still don't know is whether it will enable exhaustive matching for union, optional, or enum types in optional static typing analyzers.

Switch itself is not something that necessary.