Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Gregory Ewing Newsgroups: comp.lang.python Subject: Re: hash values and equality Date: Sat, 21 May 2011 20:24:21 +1200 Lines: 21 Message-ID: <93pb5nF3gtU1@mid.individual.net> References: <4DD2C2A5.3080403@stoneleaf.us> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> <0deha8-6u9.ln1@satorlaser.homedns.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 1zN7iOZ8Aoa0VsnIjoen1QU6kvpKDF8jVaYjsKurTdYbgWe9IY Cancel-Lock: sha1:acGV/RncsyACi9GTgKJlj/MKUfA= User-Agent: Mozilla Thunderbird 1.0.5 (Macintosh/20050711) X-Accept-Language: en-us, en In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5906 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