Re:What Is the Switch-Case Equivalent in Python?
by oblvious-earth on Wednesday April 07, 2021 @08:38PM
(#26733613)
The second version also effectively introduces a new DSL sub-language, it just puts a walrus operator at the end of it.
I agree the matching is complex, you only have to see what a portion of the grammar file it takes up: https://docs.python.org/3.10/reference/grammar.html
But embedding the match constructs inside existing Python symbols wouldn't reduce the amount of new grammar it would require.
Re:What Is the Switch-Case Equivalent in Python?
by otabdeveloper4 on Thursday April 08, 2021 @03:40AM
(#26736100)
The walrus operator is not the point; the existing constructs should be expanded, that way things make sense and are composable.
E.g., pattern matching of this sort
for ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"] in list_of_get_commads:
...
IMO should have "just worked".But now we have two different and incompatible versions of pattern matching in Python.
Re:What Is the Switch-Case Equivalent in Python?
by oblvious-earth on Thursday April 08, 2021 @02:43PM
(#26742251)
That's not an existing construct, it's a LHS assignment language like the match statement but it's embedded inside a for-loop.
Which means you have to implement all the grammar of the current match and it restricts to only works on iterables.
How would you implement this for an object that isn't iterable?
The first version is certainly cleaner, but introduces, effectively, a new DSL sublanguage.