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': <Purchases object at 0xb7ce14d0>,
'name': 'Joe Consumer',
'rewardsID': 391187}
+ {'name': 'Jane Consumer', 'rewardsID': 445137}
)
Having d['name'] to be 'Joe ConsumerJane Consumer' and d['rewardsID'] to
be 836324 would be the very opposite of useful behaviour.
--
Steven
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/EUX3R52GLBBFS4EUVOXAYFST73UMIYKF/
Code of Conduct: http://python.org/psf/codeofconduct/