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


Groups > comp.lang.python > #5883

Re: hash values and equality

Date 2011-05-20 14:48 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: hash values and equality
References (6 earlier) <BANLkTi=_GqrNntRU7pv7x=kj3gHurk-Gaw@mail.gmail.com> <mailman.1822.1305870290.9059.python-list@python.org> <ir5292$3md$1@solani.org> <4DD6AB96.2040307@stoneleaf.us> <ir6ing$et2$1@dough.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.1862.1305927371.9059.python-list@python.org> (permalink)

Show all headers | View raw


Peter Otten wrote:
> Ethan Furman wrote:
> 
>> Peter Otten wrote:
>>> 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__
>>>>
>>>> --> class Wierd():
>>>> ...     def __init__(self, value):
>>>> ...         self.value = value
>>>> ...     def __eq__(self, other):
>>>> ...         return self.value == other
>>>> ...     def __hash__(self):
>>>> ...         return hash((self.value + 13) ** 3)
>>>> ...
>>> Try this:
>>>
>>>>>> d = {Wierd(1): 0}
>>>>>> 1 in d
>>> False
>>>>>> 1 in d.keys()
>>> True
>>>
>> My apologies -- I'm trying this in Python3:
> 
> Then you have to convert the keys to a list explicitly:
> 
>>>> class Weird:
> ...     def __init__(self, value):
> ...             self.value = value
> ...     def __eq__(self, other):
> ...             return self.value == other
> ...     def __hash__(self):
> ...             return hash((self.value + 13) **3)
> ...
>>>> d = {Weird(1): 0}
>>>> 1 in d
> False
>>>> 1 in d.keys()
> False
>>>> 1 in list(d.keys())
> True

Ah!!  The light finally dawned!  Many thanks for everyone's input.

So if Wierd has a need to compare equal to some other type, it should 
implement a .equals() method.  Gotcha.

Likewise, if two different type's instances can compare equal, then for 
the most part they should be interchangeable.

~Ethan~

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