Python 3.10 is adding a new feature called Structural Pattern Matching, which is defined in PEP 634 and has a tutorial on the topic in PEP 636. Structural Pattern Matching brings the case / switch statement to Python. The new syntax goes beyond what some languages use for their case statements.

This tutorial’s aim is to get you acquainted with the new syntax that you can use in Python 3.10. But before you dive into this latest incarnation of Python, let’s review what you could use before 3.10 came out

Python Before 3.10

Python has always had several solutions that you could use instead of a case or switch statement. A popular example is to use Python’s ifelifelseas mentioned in this StackOverflow answer. In that answer, it shows the following example:

if x == 'a':
    # Do the thing
elif x == 'b':
    # Do the other thing
if x in 'bc':
    # Fall-through by not using elif, but now the default case includes case 'a'!
elif x in 'xyz':
    # Do yet another thing
else:
    # Do the default

This is a pretty reasonable alternative to using a case statement.

Another common solution that you’ll find on StackOverflow and other websites is to use Python’s dictionary to do something like this:

choices = {'a': 1, 'b': 2}
result = choices.get(key, 'default')

There are other solutions that use lambdas inside of dictionaries or functions inside of dictionaries. These are also valid solutions.

Using the ifelifelse is quite possibly the most common and is also usually the most readable solution before the release of Python 3.10.

Getting Started with Structural Pattern Matching

Python’s new structural pattern matching uses two new keywords:

  • match (not switch!)
  • case

To see how to use this code, see the following example that is based on Guido’s tutorial:

>>> status_code = 400
>>> match status_code:
...     case 400:
...         print("bad request")
...     case 200:
...         print("good")
... case _:
print("Something else bad happened") bad request

This code takes the status_code and tells Python to match it against one of the cases. If the case is _ (underscore), then the case was not found and that is the default case. That last case statement is sometimes called the “fall-through” case.

Combining Literals

You can simplify your case statements a bit by combining the literals that you are comparing against. For example, you might want to check if the pattern, status_code, matches against multiple literals. To do that, you would modify your code like this: case 400|401|403

Here’s a full example:

>>> status_code = 400 
>>> match status_code: 
...     case 400|401|403 : 
...         print("bad request") 
...     case 200: 
...         print("good")
...     case _:
            print("Something else bad happened") bad request
bad request

Isn’t that cool?

Wrapping Up

Structural Pattern Matching is an exciting new feature that is only available in Python 3.10 and newer. It’s a powerful new feature that has lots of interesting uses. Could those use-cases be solved using Python’s existing features? Probably, but this makes it even easier!

Related Articles

The post Case / Switch Comes to Python in 3.10 appeared first on Mouse Vs Python.

Categories: Python