Steve Dower <[email protected]> added the comment:
Not sure if this is a big deal or not, and it seems likely that the preexisting
behaviour of .update() and ** unpacking have already decided it, but is it
intentional that you end up with the first-seen key and the last-seen value in
the case of collisions?
class C:
def __init__(self, *a): self.a = a
def __hash__(self): return hash(self.a[0])
def __eq__(self, o): return self.a[0] == o.a[0]
def __repr__(self): return f"C{self.a}"
>>> c1 = C(1, 1); c1
C(1, 1)
>>> c2 = C(1, 2); c2
C(1, 2)
For set union we get the first seen value:
>>> {c1} | {c2}
{C(1, 1)}
For dict union we get the first seen key and the last seen value:
>>> {c1: 'a'} | {c2: 'b'}
{C(1, 1): 'b'}
But similarly for dict unpack (and .update(); code left as an exercise to the
reader):
>>> {**{c1: 'a'}, **{c2: 'b'}}
{C(1, 1): 'b'}
So the union of two dicts may contain .items() elements that were not in either
of the inputs.
Honestly, I've never noticed this before, as the only time I create equivalent
objects with meaningfully-distinct identities is to use with sets. I just
figured I'd try it out after seeing suggestions that the dict union operands
were transposed from set union.
----------
nosy: +steve.dower
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36144>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com