On Sat, Oct 19, 2019 at 02:02:43PM -0400, David Mertz wrote:
The plus operation on two dictionaries feels far more natural as a vectorised merge, were it to mean anything. E.g., I'd expect
{'a': 5, 'b': 4} + {'a': 3, 'b': 1} {'a': 8, 'b': 5}
Outside of Counter when would this behaviour be useful?
I expect that this feels natural to you because you're thinking about
simple (dare I say "toy"?) examples like the above, rather than
practical use-cases like "merging multiple preferences":
prefs = defaults + system_prefs + user_prefs
# or if you prefer the alternative syntax
prefs = defaults | system_prefs | user_prefs
(Note that in this case, the lack of commutativity is a good thing: we
want the last seen value to win.)
Dicts are a key:value store, not a multiset, and outside of specialised
subclasses like Counter, we can't expect that adding the values is
meaningful or even possible. "Adding the values" is too specialised and
not general enough for dicts, as a slightly less toy example might show:
d = ({'customerID': 12932063,
'purchaseHistory':