Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(using': 0.05; 'python': 0.08; '(unless': 0.09; '>>>>': 0.09; 'case).': 0.09; 'closer': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'hash': 0.09; 'message- id:@stoneleaf.us': 0.09; 'nameerror:': 0.09; 'now?': 0.09; 'received:gator410.hostgator.com': 0.09; 'url:dev': 0.09; '~ethan~': 0.09; 'pm,': 0.10; 'def': 0.12; 'wrote:': 0.14; 'defined': 0.14; 'cc:name:python list': 0.16; 'differently:': 0.16; 'equal,': 0.16; 'furman': 0.16; 'keyerror:': 0.16; 'n):': 0.16; 'rebert': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'traceback': 0.16; '(most': 0.16; 'cc:addr:python-list': 0.17; '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; 'tried': 0.27; 'example': 0.27; "i'm": 0.27; 'forgot': 0.29; '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; 'header:User-Agent:1': 0.35; '"",': 0.35; 'instances': 0.35; 'considered': 0.36; 'several': 0.36; 'sequence': 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; 'happen': 0.60; 'more': 0.60; 'received:websitewelcome.com': 0.67; 'writers': 0.67; 'care': 0.72; 'received:gateway02.websitewelcome.com': 0.95 Date: Fri, 20 May 2011 10:56:02 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Chris Rebert Subject: Re: hash values and equality References: <4DD2C2A5.3080403@stoneleaf.us> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> <4DD5FF8F.604@stoneleaf.us> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1701 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: 72 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305913428 news.xs4all.nl 49179 [::ffff:82.94.164.166]:37042 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5862 Chris Rebert wrote: > On Thu, May 19, 2011 at 10:43 PM, 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? Here's a toy example of a class I'm thinking of writing >> that will compare equal with int's, but hash differently: > > This is the part considered "horrible": >> --> d >> {: '3', >> 1: '1.0', >> 2: '2.0', >> 3: '3.0', >> : '2', >> : '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 "", 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). I tried this sequence (using Python 3, BTW -- forgot to mention that little tidbit -- sorry!): --> del two --> two Traceback (most recent call last): File "", line 1, in NameError: name 'two' is not defined --> d {<__main__.Wierd object at 0x00C0C950>: '3', 1: '1.0', 2: '2.0', 3: '3.0', <__main__.Wierd object at 0x00B3AC10>: '2', <__main__.Wierd object at 0x00B32E90>: '1'} --> new_two = Wierd(2) --> d[new_two] '2' ~Ethan~