Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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; 'bits': 0.07; 'subject:adding': 0.07; 'python': 0.08; 'ids': 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; 'algorithm': 0.13; 'duplicates': 0.16; "objects'": 0.16; 'python2.6': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:set': 0.16; 'unhashable': 0.16; 'wrote:': 0.18; 'appears': 0.19; 'changed': 0.23; 'default,': 0.23; 'from:addr:web.de': 0.23; 'ones.': 0.23; '(in': 0.26; 'print': 0.29; 'equality': 0.30; 'fewer': 0.30; 'hash': 0.30; 'chris': 0.30; 'least': 0.30; 'value.': 0.32; 'comment': 0.32; "can't": 0.32; 'header:X-Complaints-To:1': 0.33; 'to:addr:python-list': 0.34; 'set.': 0.34; 'checks': 0.37; 'two': 0.37; 'using': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.40; 'according': 0.61; 'your': 0.61; 'alike,': 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 18:32:04 +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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323365536 news.xs4all.nl 6860 [2001:888:2000:d::a6]:55418 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16853 Chris Angelico wrote: > It checks for equality using hashes. By default, in Python 2, objects' > hashes are their ids - meaning that no two of them hash alike, and > you'll get duplicates in your set. (In Python 3, the default appears > to be that they're unhashable and hence can't go into the set at all.) $ python3.2 -c'print({object(), object()})' {, } 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. $ python2.6 -c'o = object(); print hash(o) == id(o)' True $ python2.7 -c'o = object(); print hash(o) == id(o)' False $ python2.7 -c'o = object(); print hash(o) == id(o)>>4 | (id(o)&0xF)<<60' True $ python3.2 -c'o = object(); print(hash(o) == id(o)>>4 | (id(o)&0xF)<<60)' True