4 ms·
btw, Python has had pattern matching for almost two years now: https://peps.python.org/pep-0622/ https://peps.python.org/pep-0622/ If you support Python 3.10+
by jomohke 3y ago
btw, Python has had pattern matching for almost two years now: https://peps.python.org/pep-0622/ https://peps.python.org/pep-0622/
If you support Python 3.10+ you can do this far more cleanly.
- chriswarbo 3y agoTrue, although pattern-matching is unfortunately based on statements, which makes it less useful than if it were an expression (e.g. it can't be used in 'lambda', can't be used in function arguments, can't be used in a dictionary definition, can't be assigned to a variable, can't be used in generator/comprehension, etc.). Also, Python requires explicit returns (unless we're in a lambda), so using a `match` statement for the above example would need multiple duplicate statements (either `return`, or assignment to a repeated variable name, or calling a repeated continuation (although that's less common, due to Python not eliminating tail calls), etc.). A chain of `elif` would also have that problem.