4 ms·
This looks cool but I want dictionary unpacking (or "destructuring") more than pattern matching, hard to live without it after JS: >>> {a, b, z} = {"a": 1,
by nikisweeting 6y ago
This looks cool but I want dictionary unpacking (or "destructuring") more than pattern matching, hard to live without it after JS:
>>> {a, b, z} = {"a": 1, "b": 2, "c": 3, "z": 4}
>>> print(a, b, z)
1 2 4
# to get object attrs we can just do
>>> {someval} = SomeObject(someval=42).__dict__()
>>> print(someval)
42
- iamevn 6y agoTechnically I think this feature would do unpacking for you, just more verbosely, like so: d = {"a": 1, "b": 2, "c": 3, "z": 4} match d: case {"a": a, "b": b, "z": z}: print(a, b, z) o = SomeObject(someval=42) match o: case SomeObject(someval=someval): print(someval) from https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud-mappings https://www.python.org/dev/peps/pep-0636/#going-to-the-cloud... and https://www.python.org/dev/peps/pep-0636/#adding-a-ui-matching-objects https://www.python.org/dev/peps/pep-0636/#adding-a-ui-matchi... it's more verbose but lets you easily add a default case to catch and handle when a key you expected to be in the dict isn't or the object doesn't actually match the object you expect.