Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5911

Re: hash values and equality

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: hash values and equality
Followup-To comp.lang.python
Date Sat, 21 May 2011 11:24:40 +0200
Organization None
Lines 29
Message-ID <ir80ci$q5u$1@solani.org> (permalink)
References <4DD2C2A5.3080403@stoneleaf.us> <BANLkTimvzZeN5dmm2xHP5xV8Kpw2Nb9kuQ@mail.gmail.com> <4DD2D89D.4000303@stoneleaf.us> <BANLkTintqgBLFtBx8+1b+R10nywuKdKHOw@mail.gmail.com> <4DD2F661.2050005@stoneleaf.us> <BANLkTikx8U4jWwLXXazpmtsL6MasDmyVyg@mail.gmail.com> <BANLkTi=_GqrNntRU7pv7x=kj3gHurk-Gaw@mail.gmail.com> <mailman.1822.1305870290.9059.python-list@python.org> <0deha8-6u9.ln1@satorlaser.homedns.org> <mailman.1850.1305915987.9059.python-list@python.org> <93pb5nF3gtU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace solani.org 1305969875 26814 eJwFwYkBwCAIA8CVtCSpjIM8+4/gHU1b+UMUOByUCxPdfi2DXw1PXAFW6bniZvU6XXSrTcYDObESIw== (21 May 2011 09:24:35 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sat, 21 May 2011 09:24:35 +0000 (UTC)
X-User-ID eJwNwgkRBDEMAzBKTvPD8aUpfwh7I7mGxKSFh/n7O2SdzpdYy7hC5aUjfvZGd3KmL9HoNS3MEXiuu0qiWPwAVGYVKA==
Cancel-Lock sha1:vcpxaLL790kb82jNQHzxAeVDmDk=
X-NNTP-Posting-Host eJwFwQkBACAIA8BKirJBHD77R/BODzaKF4qrT9+S4FUB3Lpi69u2YBN1jN49I8KQHeI9TmZZDBArV/bJwgTS56gWjQF+yO4aMw==
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5911

Followups directed to: comp.lang.python

Show key headers only | View raw


Gregory Ewing wrote:

> 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.

I think for every potential match the current dict implementation checks 
identity, then hashes -- and equality only if the hash values are equal. The 
relevant function is lookdict() in dictobject.c.

While that means that the problem you describe cannot occur a simple 
approach that avoids relying on an implementation detail (?) would be to use 
(hash(obj), obj) tuples instead of just obj as the key.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

hash values and equality Ethan Furman <ethan@stoneleaf.us> - 2011-05-19 22:43 -0700
  Re: hash values and equality Peter Otten <__peter__@web.de> - 2011-05-20 08:38 +0200
    Re: hash values and equality Ethan Furman <ethan@stoneleaf.us> - 2011-05-20 10:57 -0700
    Re: hash values and equality Peter Otten <__peter__@web.de> - 2011-05-20 22:25 +0200
    Re: hash values and equality Ethan Furman <ethan@stoneleaf.us> - 2011-05-20 14:48 -0700
  Re: hash values and equality Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-05-20 08:33 +0200
    Re: hash values and equality MRAB <python@mrabarnett.plus.com> - 2011-05-20 16:50 +0100
    Re: hash values and equality Chris Angelico <rosuav@gmail.com> - 2011-05-21 02:20 +1000
    Re: hash values and equality Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-20 11:59 -0600
    Re: hash values and equality Ethan Furman <ethan@stoneleaf.us> - 2011-05-20 11:38 -0700
      Re: hash values and equality Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-05-21 20:24 +1200
        Re: hash values and equality Peter Otten <__peter__@web.de> - 2011-05-21 11:24 +0200
    Re: hash values and equality Christian Heimes <lists@cheimes.de> - 2011-05-20 21:01 +0200
    Re: hash values and equality MRAB <python@mrabarnett.plus.com> - 2011-05-20 21:17 +0100
      Re: hash values and equality Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-21 00:47 +0000
        Re: hash values and equality MRAB <python@mrabarnett.plus.com> - 2011-05-21 02:02 +0100
          Re: hash values and equality Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-21 01:55 +0000
    Re: hash values and equality John Nagle <nagle@animats.com> - 2011-05-21 15:55 -0700
      Re: hash values and equality Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-05-22 01:07 +0200
      Re: hash values and equality Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-22 00:24 +0000

csiph-web