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


Groups > comp.lang.python > #5822

Re: hash values and equality

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '(unless': 0.09; 'case).': 0.09; 'closer': 0.09; 'hash': 0.09; 'now?': 0.09; 'url:dev': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; 'cc:name:python list': 0.16; 'differently:': 0.16; 'equal,': 0.16; 'furman': 0.16; 'keyerror:': 0.16; 'n):': 0.16; 'traceback': 0.16; '(most': 0.16; 'cc:addr:python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'thu,': 0.22; 'cc:2**0': 0.22; 'keys': 0.23; 'last):': 0.23; 'objects': 0.23; 'somebody': 0.25; '(and': 0.25; 'compare': 0.26; 'object': 0.26; 'example': 0.27; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'class': 0.29; 'cc:addr:python.org': 0.30; 'equal': 0.31; "can't": 0.32; "isn't": 0.33; '...': 0.34; 'chris': 0.34; 'file': 0.34; 'thinking': 0.34; "we're": 0.34; '"",': 0.35; 'instances': 0.35; 'considered': 0.36; 'several': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:docs': 0.37; 'case': 0.37; 'two': 0.37; 'url:python': 0.38; 'hoping': 0.38; 'url:org': 0.38; 'but': 0.38; 'docs': 0.38; 'ok,': 0.38; 'subject:: ': 0.38; 'said': 0.39; 'received:209': 0.39; 'happen': 0.60; 'more': 0.60; 'writers': 0.67; 'care': 0.72; '13)': 0.84; 'sender:addr:chris': 0.84; 'received:209.85.218.46': 0.91; 'received:mail-yi0-f46.google.com': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=q9yYf7/FQVEfxZcNxZ79qpE5Mtk9RBXnIzMq87fw8RM=; b=Aqh5q8Ypg63zEZx/0XXfaUskxc7r89BuKrmdjp0bX4cyyljjcEKY7Je9yTEAx5sIzL YgsH3AdxFIAnR1HNUKTRf9IDoD8r/POX6puzPLdZv2WrnQdXa7mquEYbZpTdu/6VQKl3 aMwQib1XYJG52kGC08301QPsPOrLd5i2teyZo=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=EFoeG43KAvPEbH4wHGfKrWHiLfA2KfbSqsTsXkmLzpEYYfwTlaRAEpvD2Xvk4t8enQ Og1ziyYFxqFMGjhKJM08QXN4Oizq/u8ZukGKvUnwYVjicIDu16JuSBRbD8vasmfTuoOb MhhLjRLV1fdaiQWA/TjLqneuwrjlV9XZqKvZg=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <4DD5FF8F.604@stoneleaf.us>
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> <4DD5FF8F.604@stoneleaf.us>
Date Thu, 19 May 2011 23:44:59 -0700
X-Google-Sender-Auth 398t9V2uqO_oVyt1w6VdPIBvs6o
Subject Re: hash values and equality
From Chris Rebert <clp2@rebertia.com>
To Ethan Furman <ethan@stoneleaf.us>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1825.1305873903.9059.python-list@python.org> (permalink)
Lines 90
NNTP-Posting-Host 82.94.164.166
X-Trace 1305873903 news.xs4all.nl 49181 [::ffff:82.94.164.166]:39394
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5822

Show key headers only | View raw


On Thu, May 19, 2011 at 10:43 PM, Ethan Furman <ethan@stoneleaf.us> 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?  Here's a toy example of a class I'm thinking of writing
> that will compare equal with int's, but hash differently:
>
> --> 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)
> ...
> --> one = Wierd(1)
> --> two = Wierd(2)
> --> three = Wierd(3)
> --> one
> <Wierd object at 0x00BFE710>
> --> one == 1
> True
> --> one == 2
> False
> --> two == 2
> True
> --> three == 3
> True
> --> d = dict()
> --> d[one] = '1'
> --> d[two] = '2'
> --> d[three] = '3'
> --> d
> {<Wierd object at 0x00BFE710>: '1',
>  <Wierd object at 0x00BFE870>: '3',
>  <Wierd object at 0x00BFE830>: '2'}
> --> d[1] = '1.0'
> --> d[2] = '2.0'
> --> d[3] = '3.0'

This is the part considered "horrible":
> --> d
> {<Wierd object at 0x00BFE870>: '3',
>  1: '1.0',
>  2: '2.0',
>  3: '3.0',
>  <Wierd object at 0x00BFE830>: '2',
>  <Wierd object at 0x00BFE710>: '1'}

Compare:
>>> x = {5.0 : 'foo'}
>>> x[5]
'foo'

Here's a more common/plausible "horrible" case closer to what the docs
writers had in mind:
>>> class Naughty(object):
...     def __init__(self, n):
...         self.n = n
...     def __eq__(self, other):
...         return self.n == other.n
...
>>> Naughty(5) == Naughty(5)
True
>>> Naughty(5) is Naughty(5)
False
>>> bad = Naughty(3)
>>> y = {bad : 'foo'}
>>> y[bad] # just happens to work
'foo'
>>> del bad
>>> # ok, how do we get to 'foo' now?
>>> y[Naughty(3)] # try the obvious way
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: <__main__.Naughty object at 0x2a1cb0>
>>> # We're screwed.

Naughty instances (and similar) can't be used sensibly as hash keys
(unless you /only/ care about object identity; this is often not the
case).

Cheers,
Chris
--
http://rebertia.com

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


Thread

Re: hash values and equality Chris Rebert <clp2@rebertia.com> - 2011-05-19 23:44 -0700

csiph-web