by airstrike · 5y ago · 0 comments
Couldn't we achieve the same functionality with a little less ambiguity using the following syntax?:
not_found = 404
match status:
case not_found:
return "Not found"
case _ as foo:
do_something(foo)
return "Match anything"
it even works for the example in PEP 365 match json_pet:
case {"type": "cat", "name": _ as name, "pattern": _ as pattern}:
return Cat(name, pattern)
case {"type": "dog", "name": _ as name, "breed": _ as breed}:
return Dog(name, breed)
case _:
raise ValueError("Not a suitable pet") match subject
case [first := _, rest := _]
return [rest, first]I would have preferred `as` instead of the walrus operator too.
There are 100s of things in Python who would be confusing to people used to other languages, this is not one of them.
This syntax is clearly an improvemen to the PEP syntax.
And parent said it's "confusing". Well, it's less confusing than the PEP syntax.
Nobody compared it to not adding it at all, or was concerned with improving the language aside from the pattern matching case here.