by BiteCode_dev · 4y ago · 0 comments
Unpacking doesn't let you specify default values if cardinality doesn't match, nor can you extract dictionaries by keys.
Since operators.itemgetter and itertools.slice are limited, I end up either adding a dependency or reinventing the wheel for every project.
I'm a bit confused by this one, but maybe I'm misunderstanding. Does this not accomplish what you're defining?
def parse_record(first=0, second=1, **all_the_others) -> dict:
return dict(first=first, second=second, **all_the_others)
dictionary = dict(first=99, third=2)
parse_record(**dictionary)
> {"first": 99, "second": 1, "third": 2} d = {'a' : 10, 'b' : 20, 'c' : 30}
{'a' : v1, 'b' : v2, **others} = d
print(v1, v2, others) # prints 10, 20, {'c' : 30}The working code in Python, as a function, would be:
def extract(mapping, *args, **kwargs):
for a in args:
yield mapping[a]
for key, default in kwargs.items():
yield mapping.get(key, default)Python really needs something like:
{foo: "default", mapping} = mapping foo = mapping.pop("foo", "default")
It removes "foo" key from mapping if any. `foo` is "default" if "foo" is missing and `mapping["foo"]` otherwise.Dictionary unpacking compatible with the match/case syntax could look like:
{"foo": foo, **rest} = mapping
It asserts that there is "foo" key in mapping and puts its value into `foo`.Also using ? For attributes that might be there and being able to chain that is sooo awesome.
These two things make for example working with nested Json in JS so much better than in Python.
We would need a ? to says "ignore AttributeError, IndexError and KeyError on that access".
However, the use of {} for set is not a problem.
First, remember than Python is much, much older than JS. Insight is 20/20. Second, you could always use a different syntax.
Functional languages that have pattern matching have wildcard matches that allow you to explicitly handle things that don't match as expected and also have tail matching for lists.
> nor can you extract dictionaries by keys
I'm not exactly sure what you mean here, but you can definitely do that for records in F# and maps/structs in Elixir. It's done all the time.
Now you can do:
import operator
foo, bar, baz = operator.itemgetter('foo', 'bar', 'baz')(mapping)
But it's verbose, inelegant, and doesn't handle missing values.