adding elements to set
Peter Otten
__peter__ at web.de
Thu Dec 8 12:32:04 EST 2011
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
More information about the Python-list
mailing list