3 ms·
The update function does not work well. It is very cumbersome to have to do an in-place update. A frequent bug I see is def my_func(d1, d2): """Returns
by Areading314 8y ago
The update function does not work well. It is very cumbersome to have to do an in-place update. A frequent bug I see is
def my_func(d1, d2):
"""Returns a merged dict"""
d1.update(d2)
return d1
The problem here is that now the d1 you have passed in has been modified to contain all the keys of d2, overriding any keys that appear in both with d2's value. Having a first-class operation that does a merge without mutating the inputs will make the language easier, not harder.
- speedplane 8y agoI agree that the update function can be cumbersome, but the "+" operator implies semantics that do not apply well to dictionaries. For example, the following is illegal in python: {1, 2, 3} + {3, 4, 5} Similarly, addition with dicts should not be allowed. The pipe operator would be a closer fit, but even that has problems because with sets it's commutative and with dicts it's not.