
Photo by Steve Harvey on Unsplash
Pattern matching dispute in Python 3.10
Seeking Patterns
Pattern matching is a symbol processing technique that uses a pattern to identify discrete structures or subsets, such as strings, trees, or graphs. It is found in functional or logic programming languages that use a match
expression to process data on the basis of its structure (e.g., in Scala [1], Rust [2], and F# [3]). A match
statement takes an expression and compares it with successive patterns, which the programmer specifies in terms of one or more cases. This method is superficially similar to a switch
statement in C, Java, or JavaScript, but far more powerful.
Python 3.10 is now also set to receive such a match
expression. The Python enhancement proposal (PEP) 634 [4] describes the implementation. More information about the plans can be found in PEP 635 [5] and PEP 636 [6]. How pattern matching is intended to work in Python 3.10 is demonstrated by the simple example in Listing 1, which compares a value with multiple literals.
Listing 1
Intended Pattern Matching
def http_error(status): match status: case 400: return "Bad request" case 401: return "Unauthorized" case 403: return "Forbidden" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something
Buy this article as PDF
(incl. VAT)