[Python-ideas] adding dictionaries

Terry Reedy tjreedy at udel.edu
Tue Jul 29 03:39:28 CEST 2014


On 7/28/2014 8:16 PM, Stephen J. Turnbull wrote:
> Alexander Heger writes:
>
>   > It seems it would be valuable to parallel the behaviour of operators
>   > already in place for collections.
>
> Mappings aren't collections.  In set theory, of course, they are
> represented as *appropriately restricted* collections, but the meaning
> of "+" as applied to mappings in mathematics varies.  For functions on
> the same domain, there's usually an element-wise meaning that's
> applied.

This assumes the same range set (of addable items) also.  If Python were 
to add d1 + d2 and d1 += d2, I think we should use this existing and 
most common definition and add values. The use cases are keyed 
collections of things that can be added, which are pretty common.
Then dict addition would have the properties of the value addition.

Example: Let sales be a mapping from salesperson to total sales (since 
whenever). Let sales_today be a mapping from saleperson to today's 
sales. Then sales = sales + sales_today, or sales += sales_today. I 
could, of course, do this today with class Sales(dict): with __add__, 
__iadd__, and probably other app-specific methods.

The issue is that there are two ways to update a mapping with an update 
mapping: replace values and combine values. Addition combines, so to me, 
dict addition, if defined, should combine.

>  For functions on different domains, I've seen it used to
> mean "apply the appropriate function on the disjoint union of the
> domains".

According to https://en.wikipedia.org/wiki/Disjoint_union, d_u has at 
least two meaning.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list