3 ms·
Do you mean like this? x = match expr: case 'a': 1 case 'b': 2 case _: 0 I think mainly it doesn't work very
by benhoyt 5y ago
Do you mean like this?
x = match expr:
case 'a':
1
case 'b':
2
case _:
0
I think mainly it doesn't work very well in Python due to indentation problems. Kind of like how lambda is restricted to single expressions, instead of blocks. Here's what they say in the rationale PEP (https://www.python.org/dev/peps/pep-0635/ https://www.python.org/dev/peps/pep-0635/):
> Statement vs. Expression. Some suggestions centered around the idea of making match an expression rather than a statement. However, this would fit poorly with Python's statement-oriented nature and lead to unusually long and complex expressions and the need to invent new syntactic constructs or break well established syntactic rules. An obvious consequence of match as an expression would be that case clauses could no longer have arbitrary blocks of code attached, but only a single expression. Overall, the strong limitations could in no way offset the slight simplification in some special use cases.
- sullyj3 5y agoI think they're right that it would be too foreign to python to be a good addition, but this really just goes to show that expression oriented languages, as opposed to statement oriented ones, are the way to go.
- marmaduke 5y agoThe debugging and instrumentation in Python is line and statement oriented, so there’d be a lot more work behind the scenes.