Assume I have a class A with a overriden hash method that returns some user-defined integer:
class A:
def __init__(self,hash):
self.hash = hash
def __hash__(self):
return self.hash
def __cmp__(self,other):
return cmp(self.hash,other.hash)
Now, at any given point in time, I’d like to have only one object with the same hash, so I maintain a set s that contains such objects of class A. My problem is the following:
s = {A(1234)}
a = A(1234)
if a in s:
# then assign the corresponding object in set s to a
How can I achieve this?
Thanks!
Don’t use a set, use a dictionary (which is also a set, in a sense).