Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 722481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:59:40+00:00 2026-05-14T05:59:40+00:00

I’m trying to create a custom object that behaves properly in set operations. I’ve

  • 0

I’m trying to create a custom object that behaves properly in set operations.

I’ve generally got it working, but I want to make sure I fully understand the implications. In particular, I’m interested in the behavior when there is additional data in the object that is not included in the equal / hash methods. It seems that in the ‘intersection’ operation, it returns the set of objects that are being compared to, where the ‘union’ operations returns the set of objects that are being compared.

To illustrate:

class MyObject:
    def __init__(self,value,meta):
        self.value = value
        self.meta = meta
    def __eq__(self,other):
        return self.value == other.value
    def __hash__(self):
        return hash(self.value)

a = MyObject('1','left')
b = MyObject('1','right')
c = MyObject('2','left')
d = MyObject('2','right')
e = MyObject('3','left')
print a == b # True
print a == c # False

for i in set([a,c,e]).intersection(set([b,d])):
    print "%s %s" % (i.value,i.meta)
#returns:
#1 right
#2 right

 for i in set([a,c,e]).union(set([b,d])):
    print "%s %s" % (i.value,i.meta)
#returns:
#1 left
#3 left
#2 left

Is this behavior documented somewhere and deterministic? If so, what is the governing principle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:59:40+00:00Added an answer on May 14, 2026 at 5:59 am

    Nope, it’s not deterministic. The problem is that you’ve broken equals’ and hash’s invariant, that two objects are equivalent when they are equal. Fix your object, don’t try to be clever and abuse how set’s implementation works. If the meta value is part of MyObject’s identity, it should be included in eq and hash.

    You can’t rely on set’s intersection to follow any order, so there is no way to easily do what you want. What you would end up doing is taking the intersection by value only, then look through all your objects for an older one to replace it with, for each one. No nice way to do it algorithmically.

    Unions are not so bad:

    ##fix the eq and hash to work correctly
    class MyObject:
        def __init__(self,value,meta):
            self.value = value
            self.meta = meta
        def __eq__(self,other):
            return self.value, self.meta == other.value, other.meta
        def __hash__(self):
            return hash((self.value, self.meta))
        def __repr__(self):
            return "%s %s" % (self.value,self.meta)
    
    a = MyObject('1','left')
    b = MyObject('1','right')
    c = MyObject('2','left')
    d = MyObject('2','right')
    e = MyObject('3','left')
    
    union =  set([a,c,e]).union(set([b,d]))
    print union
    #set([2 left, 2 right, 1 left, 3 left, 1 right])
    
    ##sort the objects, so that older objs come before the newer equivalents
    sl = sorted(union, key= lambda x: (x.value, x.meta) )
    print sl
    #[1 left, 1 right, 2 left, 2 right, 3 left]
    import itertools
    ##group the objects by value, groupby needs the objs to be in order to do this
    filtered = itertools.groupby(sl, lambda x: x.value)
    ##make a list of the oldest (first in group)
    oldest = [ next(group) for key, group in filtered]
    print oldest
    #[1 left, 2 left, 3 left]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.