On 10/18/2019 10:25 AM, Steven D'Aprano wrote:
On Fri, Oct 18, 2019 at 09:17:54AM -0700, Ethan Furman wrote:
That result just doesn't match up with the '+' operator.
Why not?
Pretty sure I answered that question in my OP. Here it is again since you
conveniently stripped it out:
The problem is that dicts are complex objects with two pieces of
information, and the results of + and += don't match what happens
when two dicts are combined. For an easy demonstration of this:
--> a = {'some_key': 'some value}
--> a += {'some_key': 'another value'}
--> a
{'some_key': 'another value'}
That result just doesn't match up with the '+' operator. For what
it's worth, I don't think the '|' operator works any better.
Before answering, please check the PEP to see if your objection has
already been raised.
What do you know, it had been!
PEP 584:
-------
Dict addition is lossy
Dict addition can lose data (values may disappear); no other form
of addition is lossy.
Response:
It isn't clear why the first part of this argument is a problem.
dict.update() may throw away values, but not keys; that is expected
behavior, and will remain expected behavior regardless of whether
it is spelled as update() or +.
It's a problem because the method is named "update", not "add", "join", or "combine".
The dict "+" operator is being turned into a deduplication operator - as if, for example:
--> [1, 2, 3] + [3, 4, 5]
[1, 2, 3, 4, 5] # not what happens
The second definition of "add" in Webster's dictionary is "to join or
unite", which matches dict merging very well.
No, it doesn't -- not in the case of duplicate keys. If we had two people named
"Steve" and joined them into a group, would you expect one of them to just
disappear? Even better, if we had two engineers (key) named Anita and Carolyn (values)
and combined them into a group, do you expect one of them to vanish?
It is perfectly natural and obvious for English speakers to think of
addition as more than just the narrow definition of numeric addition.
Indeed it is, and indeed I do. You may notice, however, that I didn't use that
argument.
If you're going to deny that standard, common understanding of "plus" as
combining, and claim that it "just doesn't match up" in defiance of
common practice, you ought to have a good reason why all those who refer
to dict addition or adding dicts are wrong to do so.
As I'm sure you are aware, natural language is inherently ambiguous. Python is not a
natural language. It is my contention that the operation of combining two data
structures together that results in data loss should not be called "+". As it
happens, the Python set() agrees:
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
--> set([1, 2, 3]) + set([2, 3, 4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set'
--
~Ethan~
_______________________________________________
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/3X54PAHIL33DGIL2ILEC7STVIWDDX5GN/
Code of Conduct: http://python.org/psf/codeofconduct/