[Python-ideas] adding dictionaries

Alexander Heger python at 2sn.net
Tue Jul 29 00:35:55 CEST 2014


> The difference is that with sets, it (at least conceptually) doesn't matter whether you keep elements from s or t when they collide, because by definition they only collide if they're equal, but with dicts, it very much matters whether you keep items from s or t when their keys collide, because the corresponding values are generally _not_ equal. So this is a false analogy; the same problem raised in the first three replies on this thread still needs to be answered: Is it obvious that the values from b should overwrite the values from a (assuming that's the rule you're suggesting, since you didn't specify; translate to the appropriate question if you want a different rule) in all real-life use cases? If not, is this so useful that the benefits in some uses outweigh the almost certain confusion in others? Without a compelling "yes" to one of those two questions, we're still at square one here; switching from + to | and making an analogy with sets doesn't help.
>
>> ... and accordingly
>>
>> D = A | B | C
>>
>> Maybe this operator is better as this equivalence is already being
>> used (for sets).  Accordingly "union(A,B)" could do a merge operation
>> and return the new dict().
>
> Wouldn't you expect a top-level union function to take any two iterables and return the union of them as a set (especially given that set.union accepts any iterable for its non-self argument)? A.union(B) seems a lot better than union(A, B).
>
> Then again, A.updated(B) or updated?A, B) might be even better, as someone suggested, because the parallel between update and updated (and between e.g. sort and sorted) is not at all problematic.

yes, one does have to deal with collisions and spell out a clear rule:
same behaviour as update().

I was less uneasy about the | operator
1) it is already used the same way for collections.Counter [this is a
quite strong constraint]
2) in shells it is used as "pipe" implying directionality - order matters

yes, you are wondering whether the order should be this or that; you
just *define* what it is, same as you do for subtraction.

Another way of looking at it is to say that even in sets you take the
second, but because they are identical it does not matter ;-)

-Alexander


More information about the Python-ideas mailing list