hash values and equality

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat May 21 04:24:21 EDT 2011


Ethan Furman wrote:
> Ulrich Eckhardt wrote:
> 
>> If two equal objects have different hashes, they 
>> will be stored in different places in the hash map. Looking for 
>> object1 will then not turn up with object2, even though they are equal. 
> 
> In this case this is the behavior I want.

You can't rely on it, though. The hash value gets reduced
modulo the size of the dict, so even if two objects have
different hashes, in some cases they will land on the same
dict slot anyway.

So an object such as you're postulating would behave
unpredictably when used as a dict key. Sometimes a lookup
using a different but equal object would find it, and
sometimes not, seemingly at random.

-- 
Greg



More information about the Python-list mailing list