What I did is obviously not something that one would want to do, rather, I was just testing out implementing __hash__ for a given class.
I wanted to see if adding a phony 'hashable' class to a dictionary, then changing it's hash value would then result in it not being able to access it.
My class looks like this:
class PhonyHash:
def __hash__(self):
val = list("A string")
return id(val) # always different
Executing the following in my IPython console:
>>> p = PhonyHash()
>>> d = { p: "a value"}
>>> hash(p) # changes hash
and then trying to access the element with d[p] works:
>>> d[p]
"a value"
I understand, this is not something that should be done, I'm really just curious as to why it works. Doesn't dict use the hash() of an object to store/retrieve it? Why is this working?
edit: as noted in the comments by @VPfB sets behave as expected, for some reason:
>>> p = PhonyHash()
>>> s = {p}
>>> p in s
False
idofpdoes not change. That's also something to considerids of mutable objects also doesn't change after you mutate them.s={p}and thenp in sreturns False.p.hashI don't see this method defined