4

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)

4
  • 1
    I think your parenthetical concession is a pretty good answer to your question. + would be a misleading way to express an ambiguous oepration. Commented Aug 16, 2016 at 10:31
  • 6
    Because the output would not have been trivial. What will happen if both dictionaries have the same key? An exception? the value will be taken from the first dictionary? maybe from the second? It's not clearly defined and therefore isn't implemented. Commented Aug 16, 2016 at 10:31
  • 8
    See here Commented Aug 16, 2016 at 10:31
  • "(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)" You really are answering your own question. Commented Aug 16, 2016 at 10:33

1 Answer 1

4

Does anyone know the historic reasons for the default behaviour?

Guido van Rossum commented that he liked update() better and thinks a + operator wouldn't read clearly in code.

FWIW, he did approve PEP 448 which gives your another way to do it using star-unpacking generalizations:

>>> a = {'a': 1, 'b': 2}
>>> b = {'c': 3, 'b': 4}
>>> {**a, **b}
{'a': 1, 'b': 4, 'c': 3}

There are several reasons why + might not be a good idea. Usually, we expect addition to be commutative, but dict addition would fail whenever there were overlapping keys with distinct values. The "normal" use case is to update only dict in-place, but the usual semantics of + would copy the contents of both inputs to create a new dict (which is somewhat wasteful).

In addition, Python has collections.ChainMap which replaces the expense of copying with a new expense of potentially having multiple lookups.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.