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: 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> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> <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 To: Ethan Furman Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, May 19, 2011 at 10:43 PM, Ethan Furman wrote: > Several folk have said that objects that compare equal must hash equal, a= nd > 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? =C2=A0Here's a toy example of a class I'm thinking of wri= ting > that will compare equal with int's, but hash differently: > > --> class Wierd(): > ... =C2=A0 =C2=A0 def __init__(self, value): > ... =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.value =3D value > ... =C2=A0 =C2=A0 def __eq__(self, other): > ... =C2=A0 =C2=A0 =C2=A0 =C2=A0 return self.value =3D=3D other > ... =C2=A0 =C2=A0 def __hash__(self): > ... =C2=A0 =C2=A0 =C2=A0 =C2=A0 return hash((self.value + 13) ** 3) > ... > --> one =3D Wierd(1) > --> two =3D Wierd(2) > --> three =3D Wierd(3) > --> one > > --> one =3D=3D 1 > True > --> one =3D=3D 2 > False > --> two =3D=3D 2 > True > --> three =3D=3D 3 > True > --> d =3D dict() > --> d[one] =3D '1' > --> d[two] =3D '2' > --> d[three] =3D '3' > --> d > {: '1', > =C2=A0: '3', > =C2=A0: '2'} > --> d[1] =3D '1.0' > --> d[2] =3D '2.0' > --> d[3] =3D '3.0' This is the part considered "horrible": > --> d > {: '3', > =C2=A01: '1.0', > =C2=A02: '2.0', > =C2=A03: '3.0', > =C2=A0: '2', > =C2=A0: '1'} Compare: >>> x =3D {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 =3D n ... def __eq__(self, other): ... return self.n =3D=3D other.n ... >>> Naughty(5) =3D=3D Naughty(5) True >>> Naughty(5) is Naughty(5) False >>> bad =3D Naughty(3) >>> y =3D {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 "", line 1, in 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