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


Groups > comp.lang.python > #5834

Re: hash values and equality

From Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Newsgroups comp.lang.python
Subject Re: hash values and equality
Followup-To comp.lang.python
Date 2011-05-20 08:33 +0200
Message-ID <0deha8-6u9.ln1@satorlaser.homedns.org> (permalink)
References (3 earlier) <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>

Followups directed to: comp.lang.python

Show all headers | View raw


Ethan Furman wrote:
> Several folk have said that objects that compare equal must hash equal,
> and the docs also state this
> http://docs.python.org/dev/reference/datamodel.html#object.__hash__
> 
> I'm hoping somebody can tell me what horrible thing will happen if this
> isn't the case?

If you were familiar with what a hash map is, you wouldn't ask. The thing is 
that the hash is used to look up the place in the map where the thing is 
stored. 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. If this is something you don't 
care about, and all you care about is identity, then I'd derive the hash 
from each object's ID.

This ID has another property which is something that is assumed for hashes, 
and your code seems a bit to get that wrong, too, and that is that the hash 
must not change. Again, the reason is that if the hash changes, the position 
in the hash map changes, too. If you then try to look up the changed object, 
it will look for it in the new place, but it won't be found because it is in 
the old place.

For that reason, it is generally useful to use immutable types like 
integers, floats, strings and tuples thereof as keys. Since you can't change 
them, you basically have the guarantee that they hash the same.

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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