Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16853 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2011-12-08 18:32 +0100 |
| Last post | 2011-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.
Re: adding elements to set Peter Otten <__peter__@web.de> - 2011-12-08 18:32 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-12-08 18:32 +0100 |
| Subject | Re: 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
Back to top | Article view | comp.lang.python
csiph-web