DEV Community

Cover image for Python 3.10 Structural Pattern Matching (Match Case)
Vinay Khatri
Vinay Khatri

Posted on

Python 3.10 Structural Pattern Matching (Match Case)

After a wait of so many updates, Python has finally introduced a switch-case like structure in its latest 3.10 version. If we talk about other programming languages like, C++, Java, and JavaScript all these three popular programming languages support switch case statement, which is an alternative to write efficient and cleaner conditional statement.
Let's see an example of Switch Case statement in C++

Switch Case Statement in C++

#include <iostream>
using namespace std;

int main() {
    int http = 200;

    switch (http) {
        case  200:
            cout<<"OK";
            break;
        case 201:
            cout<<"CREATED";
            break;
        case 202:
            cout<<"ACCEPTED";
            break;
        case 400:
            cout<<"BAD REQUEST";
            break;
        case 404:
            cout<<"NOT FOUND";
            break;
        case 503:
            cout<<"SERVICE UNAVAILABLE";
            break;
        default:
            cout << "Error! HTTP Code is not valid";
            break;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT
ok

The above code is very clean example of conditional statement. The switch case comes very handy when we have a conditional operation based on a single data object.

Python Structural Pattern Matching

In Python 3.10, the core developer team of Python introduced a new conditional statement syntax Structural Pattern Matching, which is a similar syntax to the switch case statement present in the other programming languages.

Although Python contain if..else and if...elif...else statements to write conditional statements, but the all new Python Structural Pattern Matching provides a more elegant and legible way to write some conditional statement.

Syntax

match subject:
    case pattern1:
        #case 1 code block
    .....
    ....
    ...
    case patternN:
        #case N code block
Enter fullscreen mode Exit fullscreen mode

Keywords

  1. match is the keyword here.
  2. subject is the value that need to be match.
  3. case is also a keyword
  4. pattern is the value that will be match with subject if any case pattern matches the match subject that case code block will be executed.

Example

http = 200

match http:
    case 200:
        print("OK")
    case 201:
        print("CREATED")
    case 202:
        print("ACCEPTED")
    case 400:
        print("BAD REQUEST")
    case 404:
        print("NOT FOUND")
    case 503:
        print("SERVICE UNAVAILABLE")
    case _:
        print("Error! HTTP Code is not valid")
Enter fullscreen mode Exit fullscreen mode

Output

OK
Enter fullscreen mode Exit fullscreen mode

Conclusion

The new Python match case statement is an alternative of the switch case statement present in the other programming languages. Although the switch case statement provide an elegant way to write conditional statement, still for complex and multilayer conditional statement it is not a good option. It can only be used when the condition expression is a single value.

Discussion (2)

Collapse
khatrivinay profile image
Vinay Khatri Author

PEP 622 says the pattern matching statement is inspired by the similar syntax found in Scala, Erlang, and other languages. And Scala itself compares its PATTERN MATCHING
syntax with Switch Case statements.
As the standard Python also does not support core Array data structure, still we compare Arrays with Python’s List which are poles apart. The objective of this article is just to tell that Python now supports a similar syntax to the Switch Case statement that is already supported by its biggest rivals.
Peace out....