How To Use Python’s New Match Statement | by Emmett Boudreau | Towards Data Science

Python 3.10 has been out for a little bit now, and has brought about a few new extra features. As with many of the releases, many of the changes to the language are not too significant and are not breaking. To be clear, this is not breaking in the traditional sense, where old code will be broken, but it is to say that code using the syntax in the future cannot use an older version of Python.
However, one of the new changes made to Python is totally new and quite significant. This change is the match statement. This statement is something that has certainly been missed in Python prior, and can really help with the control flow of the language. Today I wanted to discuss the usage of that statement and go a bit more into detail on it.
If you are a C or C++ programmer, it is likely that you have used or at least heard of using the match statement, just that it is renamed to the switch statement. This statement is used in order to control the flow of code execution. The statement can take a value and match it elegantly against one of a number of possible conditions. This is also called pattern matching, which is a term Rust programmers might be familiar with. The statement can be used a lot like a conditional statement, and basically follows a very similar syntax to those:
from enum import Enumclass Command(Enum):    QUIT = 0    RESET = 1match command:    case Command.QUIT:        quit()    case Command.RESET:        reset()
We can match any condition and quickly move the control flow either into the block or beyond the block. While it is certainly possible to do so with simple conditionals, the match/case method is a lot prettier, and even out-performs using traditional conditional statements. Although it is a simple feature, it is quite welcome and rather cool for Python programmers.
Now that we have used the match statement, let us talk a bit more about it. You might have realized that when we compared the match statement to other programming languages with similar types of expressions were all systems/low-level, imperative programming languages. The match statement itself falls into a very imperative area of computing. So that poses a very interesting question, why is it now in Python of all languages?
We should consider that a lot of Python programmers might also write C++ or C, and that could be a valid reason in that switching back and forth, in some cases it could be missed. But the question I want to pose is whether or not this means that Python will be becoming more imperative in the future. Of course, we can expect that Python is going to remain quite declarative and high-level, but it would be interesting to speculate whether or not Python is going to be more usable on the lower-level side of things in the future. Of course, there is no way to know, but I would love to see more iterative features like this implemented in the future, as I think it really can serve to make the language a lot more versatile.
One concern that some might have is in naming. There are a lot of legacy functions written in Python 3 that use match and case as variable names, so does that make this addition of the two a breaking change to that code? The short answer is yes, you can still use match and case as variable names. Match and case have become what are called soft keywords. This means that most of the time, they have no special meaning, but when used in certain applications, such as the a case block, they become key-words that the Python interpreter uses.
Another weird thing to note about match and case is that it will not work with underscores. Underscores are another soft key-word in Python for wildcard variables, but in this one case, we can pass it through the case/match statement, but it will not alter the value of our match.
The last question is perhaps the most important, and that is of scope. It is common for blocks of code in Python to have their own scope, so what scope is a variable declared in if we use it in a match block? Case blocks in Python do not have their own scope. This means that variables will be applied to the nearest available local scope. In other words, we can define variables inside of these blocks without initialization and use them around the function, which is something you might not expect given that this does not happen in most other circumstances.
The match statement is certainly a cool new feature in the Python programming language. As to whether or not this hints at more features potentially coming in that follow this methodology, I am really excited to see how it turns out. The statement is certainly a welcome addition, though it does not necessarily provide that much new functionality. Another assumption we could make is that this could become a regular key-word in Python 4, once the language is ready to make breaking changes. Regardless, I think it is an interesting addition that I am going to be using from time to time in the future. Thank you for reading, and I hope you learned something new about Python’s latest feature!