In python - the operator which a set uses for test if an object is in the set
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping
--
Chapters
00:00 Question
01:01 Accepted answer (Score 9)
01:25 Thank you
--
Full question
https://stackoverflow.com/questions/8675...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #object #set #overriding
#avk47
    --
Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping
--
Chapters
00:00 Question
01:01 Accepted answer (Score 9)
01:25 Thank you
--
Full question
https://stackoverflow.com/questions/8675...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #object #set #overriding
#avk47
ACCEPTED ANSWER
Score 9
set uses __hash__ for comparison.  Override that, and you'll be good:
class MyClass(object):
    def __init__(self, s):
        self.s = s
    def __cmp__(self, other):
        return cmp(self.s, other.s)
    def __hash__(self):
        return hash(self.s) # Use default hash for 'self.s'
instance1, instance2 = MyClass("a"), MyClass("a")
instance3 = MyClass("b")
print instance2==instance1             # True
print instance2 in [instance1]         # True
print instance2 in set([instance1])    # True