Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!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; 'bits': 0.07; 'subject:adding': 0.07; 'unexpected': 0.07; 'python': 0.08; '>>>>': 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; 'subclass': 0.09; 'subclasses': 0.09; 'am,': 0.12; 'def': 0.13; 'algorithm': 0.13; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:set': 0.16; 'type:': 0.16; 'unhashable': 0.16; 'this:': 0.16; 'wrote:': 0.18; '>>>': 0.18; '3.2': 0.18; 'seems': 0.20; '(most': 0.21; 'dec': 0.22; 'changed': 0.23; 'from:addr:web.de': 0.23; 'ones.': 0.23; 'traceback': 0.24; '(in': 0.26; '(this': 0.28; 'pass': 0.29; 'fine.': 0.29; 'idle': 0.29; 'class': 0.29; 'fewer': 0.30; 'hash': 0.30; 'typeerror:': 0.30; 'chris': 0.30; 'least': 0.30; 'value.': 0.32; 'comment': 0.32; 'implement': 0.32; "can't": 0.32; 'header:X-Complaints-To:1': 0.33; 'fri,': 0.34; 'to:addr:python-list': 0.34; 'last):': 0.34; 'however,': 0.36; 'file': 0.36; 'but': 0.37; 'skip:_ 10': 0.37; 'received:org': 0.38; 'to:addr:python.org': 0.40; 'according': 0.61; '2011': 0.61; 'easily.': 0.67; 'saw': 0.67; '__eq__(self,': 0.84; 'interesting,': 0.84; 'other):': 0.84; 'otten': 0.84; 'x):': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: adding elements to set Date: Thu, 08 Dec 2011 19:43:01 +0100 Organization: None References: <4EE0E72E.2040104@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849e1d.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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323369791 news.xs4all.nl 6862 [2001:888:2000:d::a6]:59329 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16860 Chris Angelico wrote: > On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__@web.de> wrote: >> The only thing that has changed (in 2.7) is the algorithm to calculate >> the hash value. The bits are rotated to turn the four least significant >> bits into the most signicant ones. According to a comment in >> Objects/objects.c the change leads to fewer hash collisions. > > Interesting, but what I saw was this: > >>>> class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > >>>> s=set() >>>> c1=C(1) >>>> s.add(c1) > Traceback (most recent call last): > File "", line 1, in > s.add(c1) > TypeError: unhashable type: 'C' > > (This is in IDLE from Python 3.2 on Windows.) > > However, s.add(object()) works fine. So subclasses don't get that. > Odd. Makes sense though - you can't get this unexpected behaviour as > easily. It seems to be even subtler: you can subclass if you don't implement __eq__(): >>> class C(object): pass ... >>> {C()} {<__main__.C object at 0x17defd0>}