Python Pattern Matching in Admin Magazine #63¶

The originally object-oriented programming language Python is to receive a new feature in version 3.10, which is mainly known from functional languages: pattern matching. The change is controversial in the Python community and has triggered a heated debate.
The originally object-oriented programming language Python is to receive a new feature in version 3.10, which is mainly known from functional languages: pattern matching. The change is controversial in the Python community and has triggered a heated debate.
Pattern matching is a symbol-processing method that uses a pattern to identify
discrete structures or subsets, e.g. strings, trees or graphs. This procedure is
found in functional or logical programming languages where a match
expression is used to process data based on its structure, for example in Scala,
Rust and
F#.
A match
statement takes an expression and compares it to successive patterns
specified as one or more cases. This is superficially similar to a switch
statement in C, Java or JavaScript, but much more powerful.
Python 3.10 is now also to receive such a match
expression. The
implementation is described in Python Enhancement Proposal PEP 634. Further
information on the plans can be found in PEP 635 and PEP 636. How pattern
matching is supposed to work in Python 3.10 is shown by this very simple
example, where a value is compared with several literals:
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 else"
In the last case of the match statement, an underscore _
acts as a
placeholder that intercepts everything. This has caused irritation among
developers because an underscore is usually used in Python before variable names
to declare them for internal use. While Python does not distinguish between
private and public variables as strictly as Java does, it is still a very widely
used convention that is also specified in the Style Guide for Python Code
PEP 8.
However, the proposed match
statement can not only check patterns, for example detect a match between the value of a variable and a given pattern, it also
rebinds the variables that match the given pattern.
This leads to the fact that in Python we suddenly have to deal with Schrödinger
constants, which only remain constant until we take a closer look at them in a
match
statement. The following example is intended to explain this:
NOT_FOUND = 404
retcode = 200
match retcode:
case NOT_FOUND:
print("not found")
print(f"Current value of {NOT_FOUND=}")
This results in the following output:
not found
Current value of NOT_FOUND=200
This behaviour leads to harsh criticism of the proposal from experienced Python developers such as Brandon Rhodes, author of «Foundations of Python Network Programming»:
‘If this poorly-designed feature is really added to Python, we lose a principle I’ve always taught students: “if you see an undocumented constant, you can always name it without changing the code’s meaning.” The Substitution Principle, learned in algebra? It’ll no longer apply.”
— Brandon Rhodes on 12 February 2021, 2:55 pm on Twitter [1]
Many long-time Python developers, however, are not only grumbling about the structural pattern-matching that is to come in Python 3.10. They generally regret developments in recent years in which more and more syntactic sugar has been sprinkled over the language. Original principles, as laid down in the Zen of Python PEP 20, would be forgotten and functional stability would be lost.
Although Python has defined a sophisticated process with the Python Enhancement Proposals (PEPs) PEP 0 that can be used to collaboratively steer the further development of Python, there is always criticism on Twitter and other social media, as is the case now with structural pattern matching. In fact, the topic has already been discussed intensively in the Python community. The Python Steering Council [2] recommended adoption of the Proposals as early as December 2020. Nevertheless, the topic only really boiled up with the adoption of the Proposals. The reason for this is surely the size and diversity of the Python community. Most programmers are probably only interested in discussions about extensions that solve their own problems. The other developments are overlooked until the PEPs are accepted. This is probably the case with structural pattern matching. It opens up solutions to problems that were hardly possible in Python before. For example, it allows data scientists to write matching parsers and compilers for which they previously had to resort to functional or logical programming languages.
With the adoption of the PEP, the discussion has now been taken into the wider
Python community. Incidentally, Brett Cannon, a member of the Python Steering
Council, pointed out in an interview [3] that the last word has not yet been
spoken: until the first beta version, there is still time for changes if
problems arise in practically used code. He also held out the possibility of
changing the meaning of _
once again.
So maybe we will be spared Schrödinger’s constants.