[Python-ideas] adding dictionaries
Terry Reedy
tjreedy at udel.edu
Sun Jul 27 03:27:04 CEST 2014
On 7/26/2014 7:34 PM, Alexander Heger wrote:
> Is there a good reason for not implementing the "+" operator for dict.update()?
As you immediate noticed, this is an incoherent request as stated. A op
B should be a new object.
> A = dict(a=1, b=1)
> B = dict(a=2, c=2)
> B += A
Since "B op= A" is *defined* as resulting in B having the value of "B op
A", with the operations possibly being done in-place if B is mutable, we
would first have to define addition on dicts.
> B
> dict(a=1, b=1, c=2)
>
> That is
>
> B += A
>
> should be equivalent to
>
> B.update(A)
>
> It would be even better if there was also a regular "addition"
> operator that is equivalent to creating a shallow copy and then
> calling update():
You have this backwards. Dict addition would have to come first, and
there are multiple possible and contextually useful definitions. The
idea of choosing anyone of them as '+' has been rejected.
As indicated, augmented dict addition would follow from the choice of
dict addition. It would not necessarily be equivalent to .update. The
addition needed to make this true would be asymmetric, like catenation.
But unlike sequence catenation, information is erased in that items in
the updated dict get subtracted. Conceptually, update is replacement
rather than just addition.
> My apologies if this has been posted
Multiple dict additions have been proposed and discussed here on
python-ideas and probably on python-list.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list