Python sets claim to store only unique elements. When adding an element to a set, an error is raised if the element does not return something on hash
, therefore, I guessed a set acted as a dictionary with hashes of the element as the keys. but when I added 2 different elements with the same hash, both of them were added within the set:
class Obj:
def __init__(self, message, value):
self.message, self.value = value
def __hash__(self):
return hash(self.message)
s = set([Obj("1", 1), Obj("1", 2)])
# set value is Obj("1", 1), Obj("1", 2)
How does Python consider 2 objects to be equal or not in a set?