Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16860 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2011-12-08 19:43 +0100 |
| Last post | 2011-12-08 19:43 +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 19:43 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-12-08 19:43 +0100 |
| Subject | Re: adding elements to set |
| Message-ID | <mailman.3442.1323369791.27778.python-list@python.org> |
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 "<pyshell#163>", line 1, in <module>
> 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>}
Back to top | Article view | comp.lang.python
csiph-web