Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'type,': 0.07; '>>>>': 0.09; "everyone's": 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'hash': 0.09; 'likewise,': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'url:dev': 0.09; '~ethan~': 0.09; 'this:': 0.10; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; 'equal,': 0.16; 'furman': 0.16; 'gotcha.': 0.16; 'input.': 0.16; 'python3:': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; "type's": 0.16; 'method.': 0.16; 'cc:addr:python-list': 0.17; 'convert': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'trying': 0.23; 'keys': 0.23; 'objects': 0.23; 'compare': 0.26; "i'm": 0.27; 'thanks': 0.28; 'class': 0.29; 'implement': 0.30; 'cc:addr:python.org': 0.30; 'equal': 0.31; 'list': 0.33; '...': 0.34; 'light': 0.34; 'header :User-Agent:1': 0.35; 'instances': 0.35; 'several': 0.36; 'url:docs': 0.37; 'two': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'said': 0.39; 'received:websitewelcome.com': 0.67; '13)': 0.84 Date: Fri, 20 May 2011 14:48:27 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Peter Otten <__peter__@web.de> Subject: Re: hash values and equality References: <4DD2C2A5.3080403@stoneleaf.us> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> <4DD6AB96.2040307@stoneleaf.us> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; 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]:2231 Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305927372 news.xs4all.nl 49176 [::ffff:82.94.164.166]:38255 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5883 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~