[Tutor] Combining dictionaries

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Sep 6 23:07:18 CEST 2005


> No, that's not what he wants.
>
> He wants c = a + b to work when a and b are dictionaries.
>
> Why is Python broken in such an obvious way?


Hi Mike:

It might not be obvious.  If a and b overlap so that they share keys, then
we might have the following situation:

    c1 = a + b
    c2 = b + a

Are c1 and c2 the same?

One possible problem is that this kind of "merging" operation on
dictionaries isn't "commutative".  And notationally, addition is supposed
to be so.  Forcing the notation of arithmetic on dictionaries is
seductive, but it can invite logical errors.

Best of wishes to you!



More information about the Tutor mailing list