Member-only story

How to Write a Switch Case in Python

Python introduced a new feature called the Match case that introduced a switch case behavior to Python code since version 3.10.6 — let’s talk about it.

Oren Cohen
2 min readSep 13, 2022

You’ll be surprised how many people are looking for a Switch Case implementation in Python. I’ve been writing Python code for about five years and have never felt that not having it was a dealbreaker in any way.

Since version 3.10.6, Python has added a new feature called Match Case. It offers a very similar behavior to traditional switch case implementations. I published a demonstration video on my Oren Codes YouTube channel. You can check it out here or read below it if you prefer the written version:

Why do we need it?

Let’s say you are contacting a server. And you have prepared paths of action whether the response is successful or not. But there are multiple possible errors, and you must address them.

--

--

Oren Cohen
Oren Cohen

Written by Oren Cohen

Software Engineer and Blogger. He/Him. Contact me: oren@thegeekwriter.com Newsletter: https://theorencohen.com Geek Peek: https://geekpeek.blog

No responses yet