I've been musing as to why the Python language's standard dict class doesn't support addition/subtraction operators such as '+' or '+=', e.g.
>>> foo = {'a': 1, 'b': 2}
>>> bar = {'c': 3, 'd': 4}
>>> foo + bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
My wishful thinking would be for the following result instead:
>>> foo + bar
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
Equally why doesn't __radd__(self, other) result in the same as self.__update__(other)?
>>> foo += bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'dict' and 'dict'
Does anyone know the historic reasons for the default behaviour?
(I concede that it could be ambiguous which value should be used in scenarios where foo and bar have the same key but different key values)
+would be a misleading way to express an ambiguous oepration.