DEV Community

Cover image for Python Switches to Match-Case
Vicki Langer
Vicki Langer

Posted on • Updated on

Python Switches to Match-Case

If reading about code with foo, bar, and math examples is the bane of your existence, keep reading. Expect examples with sweaters and dogs.

Jump to:

Past

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.

Present

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.

I think it’s time for an example. Is it time to play with your dog?

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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.

Comparison if-else vs match-case .

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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.

Future

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.

What other examples can you think of? Can you come up with some real life examples? Comment below with what you come up with.

Top comments (5)

Collapse
 
gjorgivarelov profile image
gjorgivarelov • Edited

I am probably missing something but:
dog_name=input(...)
and then later in the "case" statement:
case True:
print("Go get it!", name)
Isn't it supposed to be "dog_name" instead of "name"?

And why were PEP people so hesitant with introducing the match-case feature?

Collapse
 
vickilanger profile image
Vicki Langer

Eek. Thanks for pointing that out. I fixed the messed up naming.

I can't speculate as to why it took so long to add a match-case. I do know in PEP 3103, they claimed there was no support for such a feature. Then, PEP 635 includes rationale and some history of pattern matching. Honestly though, none of this really answers your question. These are all the official answers. I was hoping to find something more informal.

Collapse
 
chorcon profile image
Chorcon

I don't think you're missing anything here, you're spot on. My guess is they changed from 'name' to 'dog_name' for clarity, then the author probably forgot to update it later in the code.

Collapse
 
vickilanger profile image
Vicki Langer

You are absolutely correct! I have since fixed the undefined variables.

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

Python has needed this for a long time. Good to see it has finally arrived. Thanks for the update 👍