Python 3.10 pattern matching: powerful but divisive
Posted by Ganesh Vinayak · 2021-06-13
Structural pattern matching arrives in Python. Love it or hate it, match/case is here. The most polarizing Python feature since the walrus operator. Here's how I'm using it for command routing. https://github.com/python/cpython
# Python 3.10 structural pattern matching
match command:
case {"action": "create", "name": str(name)}:
create_resource(name)
case {"action": "delete", "id": int(id)}:
delete_resource(id)
case {"action": "list", "filter": {"status": status}}:
list_resources(status=status)
case _:
raise ValueError(f"Unknown command: {command}")