Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16853 > unrolled thread

Re: adding elements to set

Started byPeter Otten <__peter__@web.de>
First post2011-12-08 18:32 +0100
Last post2011-12-08 18:32 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: adding elements to set Peter Otten <__peter__@web.de> - 2011-12-08 18:32 +0100

#16853 — Re: adding elements to set

FromPeter Otten <__peter__@web.de>
Date2011-12-08 18:32 +0100
SubjectRe: adding elements to set
Message-ID<mailman.3436.1323365536.27778.python-list@python.org>
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()})'
{<object object at 0x269dd00>, <object object at 0x269db60>}

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web