Originally posted on dev.
If reading about code with
bar, and math examples is the bane of your existence, keep reading. Expect examples with sweaters and dogs.
Once upon a time, in 2006, a Python Enhancement Proposal(PEP) was introduced. PEP 3103 detailed many different ways a switch statement could be implemented and why Python should have them. That’s a long time ago though. Let’s jump into the here and now.
The possibility to use match-case statements in Python has not been around very long. The addition of match-case statements in based on PEP 634. They were introduced in October of 2021 with Python v3.10. If you are using Python v3.9 or older, you won’t be able to try these out. I don’t suspect you’ll be missing out on much as these are relatively new. By the time you need them, I’m sure you’ll pick them up quickly. Either way, keep reading just to get a quick introduction to the concept. If you skip reading this, you won’t miss anything other than some cute examples. I’d suggest at least checking those out.
Worth noting, if you hear others talk about switch-case statements, match-case is pretty much the same thing.
Match-case statements are incredibly similar to if-else statements. Seemingly, they can be used interchangeably. Match-case does have a few benefits though. Your computer can read and understand match-case statements quicker than if-else statements. On that note, it tends to be easier for you and other programmers to read and manage match-case statements.
dog_name = input("What is your dog's name?") dog_wants_to_play = True # they always want to play! match dog_wants_to_play: case True: print("Go get it", dog_name) case False: print("Okay", dog_name, "maybe we'll play later")
Wait, but how would that look as an if-else statement?
dog_name = input("What is your dog's name?") dog_wants_to_play = True # they still always want to play! if dog_wants_to_play: print("Go get it", dog_name) else: print("Okay", dog_name, "maybe we'll play later")
Well, that doesn’t seem very useful. When making a decision that only has two options, an if-else statement is actually shorter. That’s okay because match-case statements really shine and show off their usefulness when we have more options.
Are you cold? I’m cold. Let’s take a trip to the Sleevonista sweater factory. Sleevonista makes one-size-fits-all sweaters with different amounts of sleeves. Here’s a long kinda unwieldy if-else example.
if sweater_sleeves == 8: print("give to spider, squid, or octopus") elif sweater_sleeves == 6: print("give to butterfly, bumble bee, Octocat™") elif sweater_sleeves == 4 or sweater_sleeves > 2: print("give to 3 or 4 legged dog") elif sweater_sleeves == 2 or sweater_sleeves == 1: print("give sweater to human with 1 or 2 arms") elif sweater_sleeves == 0: print("give sweater to to your snake friend") else: print("sweater is broken, make another one")
Let’s turn Sleevonista’s into a match-case statment.
sweater_sleeves = int(input("How many sleeves does the sweater have?")) match sweater_sleeves: case 8: print("give to spider, squid, or octopus") case 6: print("give to butterfly, bumble bee, Octocat™") case 3 | 4: print("give to 3 or 4 legged dog") case 2 | 1: print("give sweater to human with 1 or 2 arms") case 0: print("give sweater to to your snake friend") case > 8: # NOTE: unsure on the accuracy of this line's syntax print("sweater is broken, make another one") case _: print("You made a sweater")
Notice the wildcard
_. This will always be True and it considered irrefutable. You may only have one irrefutable case and it has to be the at the end.
As time goes by, I imagine match-case statements in python will catch on. Eventually, the’ll be as commonplace as switch-case statements in languages like Ruby, Java, and so many other languages. If you’d like more read more on the motivation and rationale behind Python now having match-case statements, checkout PEP 635.