On Thu, Mar 21, 2019, 10:15 PM Steven D'Aprano <steve@pearwood.info> wrote:
What would be the most useful behaviour for dict "addition" in your
opinion?

Probably what I would use most often was a "lossless" merging in which duplicate keys resulted in the corresponding value becoming a set containing all the merged values. E.g.

>>> d1 = {1: 55, 2: 77, 3: 88}
>>> d2 = {3: 99, 4: 22}
>>> add(d1, d2)
{1: 55, 2: 77, 3: {88, 99}, 4:22}

I'm sure most users would hate this too. It changes the type of values between a thing and a set of things, and that has to be sorted out downstream. But it is lossless in a similar way to Counter or sequence addition.

I can write what I want perfectly well. Perhaps useing defaultdict as a shortcut to get there. And I know there are some behaviors I have not specified here, but my function can do whatever I want in the edge cases.

If we're to see 'd1 + d2' for the first time without having followed this discussion, my guess would be behavior similar to what I show.


> Of course I could learn it and teach it, but it will always feel
> like a wart in the language.

Would that wartness be lessoned if it were spelled | or << instead?

Yes, definitely. Both those spellings feel pretty natural to me. They don't have the misleading associations '+' carries. I'm kinda fond of '<<' because it visitation resembles an arrow that I can think of as "put the stuff here into there".

> In contrast, once you tell me about the special object "vectorised arrays",
> `arr1 + arr2` does exactly what is expect in NumPy.

I don't know Numpy well enough to know whether that is elementwise addition or concatenation or something else, so that example doesn't resonate with me. I can't guess what you expect, and I have no confidence that my guess (matrix addition of equal-sized arrays, an exception if unequal) will be what Numpy does

Fair enough. I've worked with NumPy long enough that perhaps I forget what my first intuition was. I accept that it's non-obvious to many users.

FWIW, I really love NumPy behavior, but it's a shift in thinking vs lists. E.g.

>>> a = array([1, 2, 3])
>>> b = array([[10, 11, 12], [100, 200, 300]])
>>> a + b
[[  11   13   15 ]
 [ 101 202 303]]

This is "broadcasting" of compatible shapes.