Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'hash': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'url:dev': 0.09; 'this:': 0.10; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; 'differently:': 0.16; 'equal,': 0.16; 'furman': 0.16; 'python3:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'convert': 0.19; 'trying': 0.23; 'keys': 0.23; 'objects': 0.23; 'somebody': 0.25; 'compare': 0.26; 'example': 0.27; "i'm": 0.27; 'class': 0.29; 'from:addr:web.de': 0.30; 'equal': 0.31; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; "isn't": 0.33; '...': 0.34; 'thinking': 0.34; 'several': 0.36; 'url:docs': 0.37; 'url:python': 0.38; 'received:org': 0.38; 'hoping': 0.38; 'url:org': 0.38; 'but': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'said': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'happen': 0.60; '13)': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: hash values and equality Date: Fri, 20 May 2011 22:25:27 +0200 Organization: None References: <4DD2C2A5.3080403@stoneleaf.us> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> <4DD6AB96.2040307@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bdc8.dip.t-dialin.net 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: 52 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305923134 news.xs4all.nl 49044 [::ffff:82.94.164.166]:58341 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5881 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__ >>> >>> 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) >>> ... >> >> 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