Python Structural Pattern Matching
Now, we are going to learn about the concept of structural pattern matching. If you are familiar with some other programming language, like C, C++, or Java, then you might be familiar with something called switch case statements. In the simplest form, we can say that the structural pattern matching behaves like that switch case statements in other languages.
So, we are now going to get into a brief introduction to structural pattern matching. But first of all, Let’s have a look at the brief syntax that we are going to follow now.
As you can see in the syntax above, we have the match keyword, and then we have the case keyword. The thing is that we are going to have some subject value following the match keyword, and we have some pattern following the case keyword. We can say that we are matching the pattern against the subject value.
Let’s have a look at a simple program, which gives a basic idea about using structural pattern matching in our programs. In the below program, we are going to take choice as an input from the user. It is basically going to be an integer, asking whether if you want milk, coffee, or tea. We would try to match the pattern according to the choice provided. Let’s have a look at the below program now.
As you can see in the above program, we have the basic program, to understand the structural pattern matching concept. We have taken the choice as a user input. Then we are trying to match the pattern against the choice. So, if the user enters 1, the first case matches, and then we are doing whatever specified. So, for a matching case, the code is written as shown in the above program. You can write more than one instruction as well.
Let’s try to execute the above program and have a look at the output, so that we can understand the concept.
- Milk
- Tea
- Coffee
Please select your choice: 1
You selected milk… here is your milk!
As you can see in the output, we entered the choice as 1, and the matching case executed accordingly. Also, if you see in the program, the last case has an underscore. This is a wildcard, which simply means that we would do this, if no case matches. So, if we execute the same program again, and give some input that does not match any case. Let’s try –
- Milk
- Tea
- Coffee
Please select your choice: 5
Sorry… this option is not recognized. only milk, tea and coffee is available!
As you can see, we again executed the program and then put such a choice, which is not there. As the result, we get the wildcard case executed.
Now, let’s go more into different things, so that we can get more about what are some different things that we can do here. Let’s have a look at one more program and let’s try to come up with something that we can do here
As you can see in the above program, we have taken the status code as user input, and then we are matching the pattern against the given subject. In the above program, you can see that in one of the cases, we have used the ‘OR’ thing. The ‘|’ symbol in the patterns combines them as alternatives. We can call this as the OR pattern. So, as and when required, we can make use of this ‘|’ symbol in our structural pattern matching.
Now, there is something called as a guard as well, with which, we can add conditions to our case. At times, we would be required to so, that before executing the matched case, we would want to check some condition. This is like we are adding some constraints in addition to the pattern. So, a guard is an expression attached to a pattern, which must evaluate to True, for the pattern to succeed. Let’s have a look at a simple program, which tries to demonstrate us, the same thing.
In the above program, we have a simple scenario. Imagine that you visit an ATM machine, and at times, it may happen that you would be allowed to withdraw only the multiples of 500. So, the above program is a very simplified approach, in which we are just taking amount as user input, and then we are matching the pattern with subject. In the first case, we are matching the amount, and then we have a guard, which is a condition, which checks if the amount is divisible by 500. So, we have attached the guard to the pattern, which must evaluate to True, for the pattern to succeed.
So, Let’s try to execute the program and observe the output.
Please enter amount to withdraw:1000
processing and verifying the withdrawal
As you can see, the amount that we have entered is 1000, which is a multiple of 500, which is why the withdrawal is processed. On the other hand, Let’s input some amount, which is not a multiple of 500.
Please enter amount to withdraw:120
Invalid amount…only multiples of 500 are allowed! please try again!
As you can see, the amount that we had input is not a multiple of 500, so the transaction is not allowed.
So, in this way, we can make use of the structural pattern matching, as and when required in our programs. You can explore the concept in more depth and use as and when required.