[Python-ideas] adding dictionaries

Alexander Heger python at 2sn.net
Sun Jul 27 04:18:48 CEST 2014


Dear Terry,

> As you immediate noticed, this is an incoherent request as stated. A op B
> should be a new object.
> [...]
> 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.

I had set out wanting to have a short form for dict.update(), hence
the apparently reversed order.
The proposed full addition does the same after first making a shallow
copy; the operator interface does define both __iadd__ and __add__.

> 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.

yes.  As I note, most uses of the "+" operator in Python are not
symmetric (commutative).

> But unlike sequence catenation, information is erased in that items in the
> updated dict get subtracted. Conceptually, update is replacement rather than
> just addition.

Yes., not being able to have multiple identical keys is the nature of
dictionaries.
This does not mean that things should not be done in the best way they
can be done.
I was considering the set union operator "|" but that is also
symmetric and may cause more confusion.

Another consideration suggested was the element-wise addition in some form.
This is the natural way of doing things for structures of fixed length
like arrays, including numpy arrays.
And this is being accepted.
In contrast, for data structures with variable length, like lists and
strings, "addition" is concatenation, and what I would see the most
natural extension for dictionaries hence is to add the keys (not the
key values or values to each other), with the common behavior to
overwrite existing keys. You do have the choice in which order you
write the operation.

It would be funny if addition of strings would add their ASCII, char,
or unicode values and return the resulting string.

Sorry for bringing up, again, the old discussion of how to add
dictionaries as part of this.



-Alexander

On 27 July 2014 11:27, Terry Reedy <tjreedy at udel.edu> wrote:
> 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
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list