Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: hash values and equality Followup-To: comp.lang.python Date: Fri, 20 May 2011 08:38:30 +0200 Organization: None Lines: 27 Message-ID: References: <4DD2C2A5.3080403@stoneleaf.us> <4DD2D89D.4000303@stoneleaf.us> <4DD2F661.2050005@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: solani.org 1305873506 3789 eJwFwYEBwDAEBMCVEjxvHLT2HyF3UL8+YQ43LDY31ZZyWg/G5fyl28XLEnKyGV+IGbK7EfUAEiwQ3A== (20 May 2011 06:38:26 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 20 May 2011 06:38:26 +0000 (UTC) X-User-ID: eJwNxMcBwDAIBLCVOFMM41DM/iMkekjZYH3F1ERXNwanMy+/c1xqslBLeJPEkKbk/qMAxeKGp4YXYoQ5xVw+Yc4VQA== Cancel-Lock: sha1:wJdRfjlR19vwFXztTf+7vpdwFL4= X-NNTP-Posting-Host: eJwFwYEBwDAEBMCVEL4yTjz2H6F3caDg5wh4bKzcPrJEp3YZaNnB+yY3i/K5Q17ZmUNSrB3OUsu6MzENLwzvKqHlL5//8DMa2Q== Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5821 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