I\'m trying to use an object as a key in a python dictionary, but it\'s behaving in a way that I can\'t quite understand.
First I create a dictionary with my object as t
Since dicts are hash tables under the hood, you need to define both __eq__
and __hash__
for that to work.
The basic rule of thumb is:
__eq__
compares equal, __hash__
must return the same hash.From your description, something like
def __hash__(self):
return hash(str(self))
should work.