Structural pattern matching with Python 3.10 (AKA "Switch/Case")
Python has been designed to be a very readable and compact programming language [since the language was born in 1991](https://en.wikipedia.org/wiki/Python_(programming_language)). Indentation is required, semicolons are not needed, readability and elegance are highly promoted and I'm sometimes still stunned how complex use-cases can be implemented so clean and simple with just a few lines of Python.But one of the things that has been controversial in the language, and a hot topic in the Python community for more than 30 (!) years, is the lack of a `switch` or `case` statement.
Python has been designed to be a very readable and compact programming language [since the language was born in 1991](https://en.wikipedia.org/wiki/Python_(programming_language)). Indentation is required, semicolons are not needed, readability and elegance are highly promoted and I'm sometimes still stunned how complex use-cases can be implemented so clean and simple with just a few lines of Python.
But one of the things that has been controversial in the language, and a hot topic in the Python community for more than 30 (!) years, is the lack of a `switch` or `case` statement.
The Python official FAQ wrote about this:
You can do this easily enough with a sequence of if... elif... elif... else.
Some history regarding the switch statement
-
In 2001, PEP-275 was submitted for Python 2 to introduce a
switchstatement in the language, but this was never accepted. -
In 2006, Guido himself submitted the introduction of a
casestatement in PEP-3103, but this was also rejected (by himself) because this proposal did not have "popular support". -
It did take up to 2020 for PEP-634 and PEP-635 to be proposed which introduces a
casestatement, including advanced structural pattern matching as found in Haskell and Ruby. -
In October 2021, a
match...casestatement was finally introduced in Python 3.10.
Let's see how it looks like with some examples:
HTTP response handler example
For the following examples, we'll use a Response class which has two properties, a status_code (like 200 or 404) and an error_code, such as "invalid-credentials":
@dataclass class Response: status_code: int error_code: str
if / elif / else
If we would make a handler to react to a specific response, we would traditionally write it like this:
def handle_response(response): if response.status_code == 400: return BadRequestHandler(response) elif response.status_code == 403: error_message = response.body.json()["message"] return UnauthorizedHandler(response, message=error_message) elif response.status_code == 500: return ServerErrorHandler(response) else: return UnknownStatusCodeHandler(response)
It does the job, but still it looks a bit messy due to the different spacing, and this kind of code just screams for a better alternative.
Dictionary mapping
For a simple handling mechanism, you could use a dictionary to map the status_code to a specific handler:
def handle_response(response): handler_mapping = { 400: BadRequestHandler, 403: UnauthorizedHandler, 500: ServerErrorHandler, } handler = handler_mapping.get( response.status_code, UnknownStatusCodeHandler) return handler(response)
Although this looks already much cleaner compared to the if... elif... else example above, it has some limitations:
- If you need to write extra code before returning the correct handler (like extracting a message from the response) you'll need to add extra helper methods, which makes things more messy.
- You are limited to the possible values of dictionary keys, no complex data structures or extra conditions are possible.
Matching statements
Rewriting the first example with the new match...case statement, it will look like this:
def handle_response_match(response): match response.status_code: case 400: return BadRequestHandler(response) case 403: error_message = response.body.json()["message"] return UnauthorizedHandler(response, message=error_message) case 500: return ServerErrorHandler(response) case _: return UnknownStatusCodeHandler(response)
It's hard to believe that this hasn't been added to the language earlier! These kind of multi-condition-statements are much more readable as the alternatives we were used too, and makes the implementation of these kind of handlers so much easier.
And there is even more, as the match statement can do some advanced matching like this:
response = { "status_code": 403, "error_code": "invalid-credentials" } def handle_response(response): match response: case {"status_code": 403, "error_code": "invalid-credentials"}: # This handles a specific 403 response return InvalidCredentialsHandler(response) case {"status_code": 403, "error_code": error_code}: # This handlers all other 403 responses return BadRequestHandler(response, error_code) ...
Or even like this:
def handle_response(response): match response: case Response(403, "invalid-credentials"): return InvalidCredentialsHandler(response) case Response(400, error_code): return NotFoundHandler(error_code) ...
When the response object would be a tuple you can even unpack the values like this:
def handle_response(response): match response: case (404, error_code): return NotFoundHandler(error_code) case (403, "invalid-credentials"): return InvalidCredentialsHandler(response) case (403, error_code): return UnauthorizedHandler(response, error_code) ...
And you can even add more conditions with guards:
def handle_response(response, user): match response: case (403, error_code) if user.authenticated: return NoPermissionHandler(response) case (403, error_code): return UnauthorizedHandler(response, error_code) ...
Final notes
Go and check out PEP-636, which contains even more great and advanced examples what you can do with structural pattern matching in Python.
If you didn't find a good reason to update to Python 3.10 before, there is one now!
Over de auteur
Rob Moorman
Iets aan gehad? Deel het artikel.
Andere artikelen
Bekijk alle artikelen-
Werk sneller met herbruikbare content in Vio
Als contentbeheerder wil je snel kunnen werken, zonder concessies te doen aan kwaliteit of consistentie. Lees er meer over in ons blog.
-
Geen plugins, wel alles wat je nodig hebt
Als contentbeheerder wil je snel kunnen werken, zonder concessies te doen aan kwaliteit of consistentie. Lees er meer over in ons blog.
-
Maak sneller betere content met de Vio AI app
Maak, corrigeer of redirigger sneller betere content met de Vio AI app. Lees er meer over in ons blog.
-
Vio vs Wordpress: een eerlijke vergelijking
WordPress is een van de bekendste CMS’en. Vio zich onderscheidt met gebruiksvriendelijkheid, veiligheid en schaalbaarheid. Lees er meer over in ons blog.
-
5 tips om je website optimaal te beveiligen
Datalekken en cyberaanvallen kunnen enorme gevolgen hebben. Vio maakt het eenvoudig om je website optimaal te beveiligen. Lees er meer over in ons blog.
-
7 Signalen dat jouw CMS dringend toe is aan vervanging
Herken je één of meer van deze signalen? Dan is het tijd voor een upgrade. Lees er meer over in ons blog.
-
Waarom updates essentieel zijn voor je website
Het up-to-date houden van een website blijft voor veel bedrijven een uitdaging. Waarom het toch essentieel is? Lees er meer over in ons blog.
-
Vio, een duurzaam en toekomstbestendig CMS
Wat maakt Vio een duurzame en toekomstbestendige keuze voor jouw organisatie? Lees er meer over in ons blog.
-
Gebruiksvriendelijkheid: essentieel voor elke organisatie
In een wereld waar snelheid en eenvoud de norm zijn, is een gebruiksvriendelijk CMS de basis voor succes. Lees er meer over in ons blog.
-
Fix Docker no space left on device
When doing a lot with Docker locally every developer find themselves with the no space left on device error message. Even though the error doesn't occur it's good to often …
Rob Moorman
-
Squash Django migrations
Maintaining a Django (and Wagtail) platform over multiple years comes with some challenges. Although Django is build to scale there are some things to consider while keeping your codebase clean. …
Rob Moorman
-
Setup a SSH tunnel
Often you need to migrate production data back to your development environment.In our case we use virtual private networks with strict private- and public subnets in order to secure our …
Rob Moorman
-
Migrating Wagtail ModelAdmin to the new snippet admin views
With the new snippet admin views the usage of modeladmin becomes outdated. The snippets now leverage the preview & history abilities from Wagtail, similar to the workflow of pages:PreviewDraft & …
Rob Moorman
-
Add Plausible to your Terraform CloudFront distribution
At Vicktor we take data very serious, that's why we have fully adopted Plausible as a simple and privacy-friendly Google Analytics alternative."Plausible is lightweight and open source web analytics. No …
Rob Moorman
-
Beautiful asserts with your Django Test Client
When you write tests using the [Django Test Client](https://docs.djangoproject.com/en/4.1/topics/testing/tools/#the-test-client) you often want to check for error messages being displayed correctly, or if a page renders specific HTML elements.
Rob Moorman
-
A Private PyPI server with AWS CodeArtifact
IntroductionWhen you develop a variety of reusable Python packages or apps you soon will be facing the limits of git dependencies. And you don't want to release company / project …
Rob Moorman