Python May Get Pattern Matching Syntax
The creators of the Python language are mulling a new proposal,PEP 622, that would finally bring a pattern matching statement syntax to Python. The new pattern matching statements would give Python programmers more expressive ways of handling structured data, without having to resort to workarounds. Pattern matching is a common feature of many programming languages, such as switch/case in C.
It allows one of a number of possible actions to be taken based on the value of a given variable or expression. While Python has lacked a native syntax for pattern matching, it has been possible to emulate it withif/elif/else chains or a dictionary lookup. PEP 622 proposes a method for matching an expression against a number of kinds of patterns using a match/case syntax: Supported pattern match types includeliterals, names, constant values, sequences, a mapping (basically, the presence of a key-value pair in the expression), a class, a mixture of the above, or any of those plus conditional expressions.
Any matches that are ambiguous or impossible to resolve will throw an exception at runtime.