Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

3.10 → 3.11 - Covering the match/case Statement

00:00 Previous lesson covered Python’s 3.8 and 3.9, and demonstrated the walrus operator and = inside of an f-string field. This lesson covers Python 3.10 and 11, and goes over the match case statement.

00:15 Python 3.10 included a new language feature, the match case statement. This is inspired by switch selection statements in other languages. Replacing certain kinds of larger if elif else blocks with the match statement can make your code more readable.

00:30 The introduction of the PEG parser has meant cleaner code inside of CPython and allowed the core contributors to add small features that can make a big difference.

00:39 That includes things like improved error messages, giving you a better idea of why something isn’t compiling properly and in some cases exactly what position in the statement is triggering the problem.

00:50 This is one of those nice little features that just makes coding better. Let’s head to the REPL and look at the match case statement.

00:57 Before getting into the new syntax, let me show you some code that you might replace with the match statement.

01:03 Declare a variable conditional check,

01:11 another conditional

01:16 and a follow-through clause.

01:19 Nothing earth-shattering here. Just a typical if elif else block. The official name for the match case statement from the PEP that defined it is structural pattern matching.

01:30 The basic idea is to examine a value and then step through a series of case statements to see if the value matches any of those conditions.

01:39 match x means to examine the contents of x in our comparison statements.

01:45 Then for each comparison condition, you use a case statement here. case 3 is similar to our first if x == 3` statement above. If the contents of x match 3, then what’s inside the case block would run.

02:03 For each comparison you want to do, you add another case statement.

02:09 A case of underscore is default. This is the fall-through case like the else clause and like with the else clause, you don’t have to have one of these. And there you have it.

02:21 The same result. So far, this isn’t much different from the if elif else clauses. In fact, at the moment it ends up being one additional line of code, but then the contents of the lines are a little less.

02:32 So it’s sort of a wash. In my school, I’ve only skimmed the surface of the pattern matching supported by the case statement. Let’s do something a little more complicated.

02:42 Say instead of an integer, you want to match a tuple.

02:47 Like before you match the variable,

02:52 but now you can unpack the contents as part of the case statement. This case essentially says is version a tuple? And if it is, unpack its contents into major and minor. You likely wouldn’t do a match with only one case like I have here, but I was only trying to demonstrate the idea of the unpacking.

03:12 You could have another case for string and another one for float for a bunch of different ways of representing version numbers.

03:18 The unpacking during the case statement is quite handy and it works with things like the data classes we were talking about before. This actually allows you to compact some of your code.

03:27 This would otherwise be multiple statements inside of your if else blocks. So this is where it tends to become a little easier.

03:34 Have you played with these yet? I love them. I think it’s my SQL background. Great. So yeah, I had done lots of select stuff, so yeah, I’ve only recently started using it in the last couple of months.

03:47 At the time of this recording, Python 3.8 is still supported. So for my libraries that are supposed to work across multiple versions, I’m not adopting some of these kinds of things yet because then it won’t work in the older versions, but I’ve been recently writing code that’s just been for myself and I can decide what I want to do and it’s been including this and I actually even used the walrus operator recently.

04:09 So yeah. Nice progress. Yeah.

04:12 In episode 105 of the Real Python podcast, Mr. Bailey here interviewed Pablo Galindo Salgado and they talked about the error message improvements that happened across 3.10 and 3.11.

04:24 So if you want to know a bit more about how they happened, give it a listen. And for more information about the new stuff in Python 3.10, you can check out my course, the tutorial, or our podcast episode on the topic.

04:38 Python 3.11 added a lot of performance improvements. This is something that you’re going to see even more going forward, but that was one of the big advertised functions and features.

04:49 And along with that, another feature that we want to highlight here is the exception groups. The grouping of exceptions was a nice functionality, and then notes for your exceptions so you can kind of add more details to what’s happening within those exceptions.

05:04 The tomllib module was added. You had to import separate tools to deal with TOML inside of Python before that. And also this is the beginning of removing some of the dead batteries that are inside of Python.

05:18 Python has always been known as this batteries included. So if you haven’t gotten that reference before, this idea that Python has so many sort of built-in modules and features and things inside of its library to handle lots of cases.

05:32 Well, after 20 some odd years, it made sense that some of these things are not being quite addressed in the same way. I remember the removal of some sound file formats and some other interesting things that definitely hadn’t been used in quite a while.

05:47 I’m a big fan of TOML. I had never heard of it before playing with it in Python. It was the introduction here that sort of educated me on it, but I haven’t turned back. Like, there have been, I think over the years in Python there’s been two or three different ways that I’ve stored configuration for my programs and now all the time it’s just TOML is the answer.

06:06 Yeah, I mean the pyproject.toml file has also kind of made this the other thing. Yes. Makes it quite the standard in Python. Yeah, you have to learn it anyways, so you might as well use it.

06:17 Yeah, totally. So yeah, if you want to learn a little more about working with TOML and Python, we have a video course and of course written tutorial. If you’d like to learn a little bit more about how the exception groups work, we have a written tutorial and continuing the trend of having the cool new features written tutorial and a video course by Mr.

06:39 Trudeau. And again, a podcast episode where the three of us discuss the features added in Python 3.11 in detail.

06:49 Next up, the performance improvements in Python 3.12 and 3.13.

Become a Member to join the conversation.