[Python-ideas] Adding "+" and "+=" operators to dict

Greg greg.ewing at canterbury.ac.nz
Thu Feb 12 05:06:33 CET 2015


On 12/02/2015 4:59 p.m., Chris Angelico wrote:
>> Addition in the usual sense of the word wouldn't be commutative for
>> dictionaries.
>
> Does it have to be? It isn't commutative for strings or tuples either.

I think associativity is the property in question, and it
does hold for string and tuple concatenation.

Dict addition could be made associative by raising an
exception on duplicate keys.

Another way would be to define {'a':x} + {'a':y} as
{'a': x + y}, but that would probably upset a lot of
people. :-)

-- 
Greg



More information about the Python-ideas mailing list