[Python-ideas] Re: PEP 584: Add + and += operators to the built-in dict class.
On 22/10/2019 06:43, Richard Musil wrote: It is not a "concatenation" though, because you lost {"key1": "val1"} in the process. The concatenation is not _just_ "writing something after something", you can do it with anything, but the actual operation, producing the result. My point is that if I saw {"key1": "val1", "key2": "val2"} + {"key1": "val3"}, I would expect that it would be equivalent to {"key1": "val1", "key2": "val2", "key1": "val3"}. Similarly, I would expect that deque([1, 2, 3], maxlen=4) + deque([4, 5]) == deque([1, 2, 3, 4, 5], maxlen=4) == deque([2, 3, 4, 5], maxlen=4) which indeed is true. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/YSSTR2ZQWTDUXM25OHHTCEWKY5LJXLPE/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: PEP 584: Add + and += operators to the built-in dict class.
On 21/10/2019 21:14, Dominik Vilsmeier wrote: Exactly, so the dict "+" behavior would match the set "|" behavior, preserving the keys. But how many users will be concerned about whether the keys are going to be preserved? I guess almost everybody will want to know what happens with the values, and that question remains unanswered by just looking at the "+" or "|" syntax. It's reasonable to assume that values are preserved as well, i.e. `d1 + d2` adds the missing keys from `d2` to `d1`. Of course, once you know that "+" is actually similar to "update" you can infer that the last value wins. There's one reason for + which I feel is being missed (though I think someone may have briefly mentioned it last time this topic was brought up): If we look at the behaviour of dict literals, adding two dicts actually behaves like concatenation in the sense that {"key1": "val1", "key2": "val2", "key1": "val3"} == {"key1": "val3", "key2": "val2"} which is exactly what we would get by adding {"key1": "val1", "key2": "val2"} and {"key1": "val3"} so using + we would actually have {"key1": "val1", "key2": "val2"} + {"key1": "val3"} == {"key1": "val1", "key2": "val2", "key1": "val3"} ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/IRU66KNSTIG2FYE2KCGTCZHWYT22HC44/ Code of Conduct: http://python.org/psf/codeofconduct/