This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steve.dower
Recipients Aaron Hall, brandtbucher, gvanrossum, josh.r, mark.dickinson, rhettinger, scoder, serhiy.storchaka, slam, steve.dower, xtreak
Date 2020-02-25.20:05:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582661125.95.0.964095002052.issue36144@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-02-25 20:05:25steve.dowersetrecipients: + steve.dower, gvanrossum, rhettinger, mark.dickinson, scoder, serhiy.storchaka, josh.r, Aaron Hall, slam, xtreak, brandtbucher
2020-02-25 20:05:25steve.dowersetmessageid: <1582661125.95.0.964095002052.issue36144@roundup.psfhosted.org>
2020-02-25 20:05:25steve.dowerlinkissue36144 messages
2020-02-25 20:05:25steve.dowercreate