I am using a hashable object as a key to a dictionary. The objects are hashable and I can store key-value-pairs in the dict
, but when I create a copy of the same object (that gives me the same hash), I get a KeyError
.
Here is some small example code:
class Object:
def __init__(self, x): self.x = x
def __hash__(self): return hash(self.x)
o1 = Object(1.)
o2 = Object(1.)
hash(o1) == hash(o2) # This is True
data = {}
data[o1] = 2.
data[o2] # Desired: This should output 2.
In my scenario above, how can I achieve that data[o2]
also returns 2.
?