Message362660
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. |
|
Date |
User |
Action |
Args |
2020-02-25 20:05:25 | steve.dower | set | recipients:
+ steve.dower, gvanrossum, rhettinger, mark.dickinson, scoder, serhiy.storchaka, josh.r, Aaron Hall, slam, xtreak, brandtbucher |
2020-02-25 20:05:25 | steve.dower | set | messageid: <1582661125.95.0.964095002052.issue36144@roundup.psfhosted.org> |
2020-02-25 20:05:25 | steve.dower | link | issue36144 messages |
2020-02-25 20:05:25 | steve.dower | create | |
|