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

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Feb 12 22:11:55 CET 2015


random832 at fastmail.us wrote:
> On Wed, Feb 11, 2015, at 23:06, Greg wrote:
>>
>>Dict addition could be made associative by raising an
>>exception on duplicate keys.
> 
> Why isn't it associative otherwise?

I wasn't thinking straight yesterday. It is of course
associative under left-operand-wins o right-operand-wins
also.

The OP was right that commutativity is the issue. It's
true that there are many non-commutative operations used
in mathematics, but there is usually some kind of symmetry
about them nonetheless. They're not biased towards one
operand or the other.

For example, sequence concatenation obeys the relation

    a + b == reversed(reversed(b) + reversed(a))

and matrix multiplication obeys

    A * B == transpose(transpose(B) * transpose(A))

There would be no such identity for dict addition under
a rule that favours one operand over the other, which
makes it seem like a bad idea to me to use an operator
such as + or | that is normally expected to be symmetrical.

-- 
Greg


More information about the Python-ideas mailing list