Dictionary union (PEP 584) is merged
21–30 of 87 posts
Re: Dictionary union (PEP 584) is merged
#22What does this do on duplicate keys? Keep one? Take a predicate?
Re: Dictionary union (PEP 584) is merged
#23What does this do on duplicate keys? Keep one? Take a predicate?
Re: Dictionary union (PEP 584) is merged
#24{**d1, **d2} is very natural if you also write javascript where their spread operator looks like: {...d1, ...d2}
Re: Dictionary union (PEP 584) is merged
#25Earlier quoted context omitted.
Guido himself said he had forgotten about this trick and since it's syntactic sugar, it does not respect dict subclasses or other mappings.
imo defaultdict(callback,{**a,**b}) is more readable than a | b without knowing that a or b are defaultdicts and having to reason about which default callback will be used
Re: Dictionary union (PEP 584) is merged
#26{**d1, **d2} is very natural if you also write javascript where their spread operator looks like: {...d1, ...d2}
So PEP 584 is a JavaScript inspired feature request?
{**d1, **d2}
because it apparently looks ugly[1].Re: Dictionary union (PEP 584) is merged
#27Re: Dictionary union (PEP 584) is merged
#28{**d1, **d2} is very natural if you also write javascript where their spread operator looks like: {...d1, ...d2}
Re: Dictionary union (PEP 584) is merged
#29Earlier quoted context omitted.
It mentions in the PEP discussion that the { a, b} trick only works for string keys. So it isn't applicable in as many cases as the new operator.
actually, only dict(d1, **d2) has that problem. it works fine if you unpack into a dict literal: >>> d1 = {1: 'a'} >>> d2 = {2: 'b'} >>> {**d1, **d2} {1: 'a', 2: 'b'} iirc the pep mostly just says that it's suboptimal because it's syntactically heavy/noisy, non-obvious and can't be overloaded in dict subclasses --- i was curious why the two double-stars behave differently despite syntactic similarity. so i went and c…
`dict(d1, d2)`
You are calling the dict function, and using the normal syntax for unpacking a dictionary into kwargs. In this case, the name for kwargs must be strings.
Re: Dictionary union (PEP 584) is merged
#30Fantastic. Especially glad they went with | over +, that’s always felt like the natural way I’ve wanted to do this. Looking forward to more set-like operators in the future!
We had a whole discussion on HN last time[1] about this, where I argued that dicts are logically subclasses of sets and therefore should share operators.
When I saw this headline I accepted my fate of typing the "wrong" operator from now on and liking Python just a tiny bit less for the inconsistency. So glad they reconsidered.