PEP 635 – Structural Pattern Matching: Motivation and Rationale | peps.python.org
PEP 635 – Structural Pattern Matching: Motivation and Rationale PEP 635 – Structural Pattern Matching: Motivation and Rationale Author : Tobias Kohn <kohnt at tobiaskohn.ch>, Guido van Rossum <guido at python.org> BDFL-Delegate : Discussions-To : Python-Dev list Status : Final Type : Informational Created : 12-Sep-2020 Python-Version : 3.10 Post-History : 22-Oct-2020, 08-Feb-2021 Resolution : Python-Committers message Table of Contents Abstract Motivation Pattern Matching and OO Patterns and Functional Style Rationale Overview and Terminology The Match Statement Match Semantics Guards Patterns AS Patterns OR Patterns Literal Patterns Capture Patterns Wildcard Pattern Value Patterns Group Patterns Sequence Patterns Mapping Patterns Class Patterns History and Context Backwards Compatibility Security Implications Reference Implementation References Copyright Abstract This PEP provides the motivation and rationale for PEP 634 (“Structural Pattern Matching: Specification”).
First-time readers are encouraged to start with PEP 636 , which provides a gentler introduction to the concepts, syntax and semantics of patterns. Motivation (Structural) pattern matching syntax is found in many languages, from Haskell, Erlang and Scala to Elixir and Ruby. (A proposal for JavaScript is also under consideration.) Python already supports a limited form of this through sequence unpacking assignments, which the new proposal leverages.
Several other common Python idioms are also relevant: The if ... elif ... elif ... else idiom is often used to find out the type or shape of an object in an ad-hoc fashion, using one or more checks like isinstance(x, cls) , hasattr(x, "attr") , len(x) == n or "key" in x as guards to select an applicable block. The block can then assume x supports the interface checked by the guard. For example: if isinstance ( x , tuple ) and len ( x ) == 2 : host , port = x mode = "http" elif isinstance ( x , tuple ) and len ( x ) == 3 : host , port , mode = x # Etc.
Code like this is more elegantly rendered using match : match x : case host , port : mode = "http" case host , port , mode : pass # Etc. AST traversal code often looks for nodes matching a given pattern, for example the code to detect a node of the shape “A + B * C” might look like this: if ( isinstance ( node , BinOp ) and node . op == "+" and isinstance ( node . right , BinOp ) and node . right . op == "*" ): a , b , c = node . left , node . right . left , node . right . right # Handle a + b*c Using match this becomes more readable: match node : case BinOp ( "+" , a , BinOp ( "*" , b , c )): # Handle a + b*c We believe that adding pattern matching to Python will enable Python users to write cleaner, more readable code for examples like those above, and many others.
For a more academic discussion to this proposal, see [1] . Pattern Matching and OO Pattern matching is complimentary to the object-oriented paradigm. Using OO and inheritance we can easily define a method on a base class that defines default behavior for a specific operation on that class, and we can override this default behavior in subclasses. We can also use the Visitor pattern to separate actions from data. But this is not sufficient for all situations.
For example, a code generator may consume an AST, and have many operations where the generated code needs to vary based not just on the class of a node, but also on the value of some class attributes, like the BinOp example above. The Visitor pattern is insufficiently flexible for this: it can only select based on the class. See a complete example . Like the Visitor pattern, pattern matching allows for a strict separation of concerns: specific actions or data processing is independent of the class hierarchy or manipulated objects.
When dealing with predefined or even built-in classes, in particular, it is often impossible to add further methods to the individual classes. Pattern matching not only relieves the programmer or class designer from the burden of the boilerplate code needed for the Visitor pattern, but is also flexible enough to directly work with built-in types. It naturally distinguishes between sequences of different lengths, which might all share the same class despite obviously differing structures.
Moreover, pattern matching automatically takes inheritance into account: a class D inheriting from C will be handled by a pattern that targets C by default. Object oriented programming is geared towards single-dispatch: it is a single instance (or the type thereof) that determines which method is to be called. This leads to a somewhat artificial situation in case of binary operators where both objects might play an equal role in deciding which implementation to use (Python addresses this through the use of reversed binary methods).
Pattern matching is structurally better suited to handle such situations of multi-dispatch, where the action to be taken depends on the types of several objects to equal parts. Patterns and Functional Style Many Python applications and libraries are not written in a consistent OO style – unlike Java, Python encourages defining functions at the top-level of a module, and for simple data structures, tuples (or named tuples or lists) and dictionaries are often used exclusively or mixed with classes or data classes.
Pattern matching is particularly suitable for picking apart such data structures. As an extreme example, it’s easy to write code that picks a JSON data structure using match : match json_pet : case { "type" : "cat" , "name" : name , "pattern" : pattern }: return Cat ( name , pattern ) case { "type" : "dog" , "name" : name , "breed" : breed }: return Dog ( name , breed ) case _ : raise ValueError ( "Not a suitable pet" ) Functional programming generally prefers a declarative style with a focus on relationships in data.
Side effects are avoided whenever possible. Pattern matching thus naturally fits and highly supports functional programming style. Rationale This section provides the rationale for individual design decisions. It takes the place of “Rejected ideas” in the standard PEP format. It is organized in sections corresponding to the specification ( PEP 634 ). Overview and Terminology Much of the power of pattern matching comes from the nesting of subpatterns.
That the success of a pattern match depends directly on the success of subpattern is thus a cornerstone of the design. However, although a pattern like P(Q(), R()) succeeds only if both subpatterns Q() and R() succeed (i.e. the success of pattern P depends on Q and R ), the pattern P is checked first. If P fails, neither Q() nor R() will be tried (this is a direct consequence of the fact that if P fails, there are no subjects to match against Q() and R() in the first place).
Also note that patterns bind names to values rather than performing an assignment. This reflects the fact that patterns aim to not have side effects, which also means that Capture or AS patterns cannot assign a value to an attribute or subscript. We thus consistently use the term ‘bind’ instead of ‘assign’ to emphasise this subtle difference between traditional assignments and name binding in patterns. The Match Statement The match statement evaluates an expression to produce a subject, finds the first pattern that matches the subject, and executes the associated block of code.
Syntactically, the match statement thus takes an expression and a sequence of case clauses, where each case clause comprises a pattern and a block of code. Since case clauses comprise a block of code, they adhere to the existing indentation scheme with the syntactic structure of <keyword> ...: <(indented) block> , which resembles a compound statement. The keyword case reflects its widespread use in pattern matching languages, ignoring those languages that use other syntactic means such as a symbol like | , because it would not fit established Python structures.
The syntax of patterns following the key
The rest of this page is on the live site.