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


Groups > comp.lang.python > #16854

Re: adding elements to set

References <4EE0E72E.2040104@gmail.com> <CAPTjJmpm9xV21WaAsaO+PysuMa=oJ6avT4x-OJWKm2jgKttH1Q@mail.gmail.com> <jbqsad$q2n$1@dough.gmane.org>
Date 2011-12-09 04:37 +1100
Subject Re: adding elements to set
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3437.1323365841.27778.python-list@python.org> (permalink)

Show all headers | View raw


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.

ChrisA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: adding elements to set Chris Angelico <rosuav@gmail.com> - 2011-12-09 04:37 +1100
  Re: adding elements to set Duncan Booth <duncan.booth@invalid.invalid> - 2011-12-08 18:54 +0000
    Re: adding elements to set Terry Reedy <tjreedy@udel.edu> - 2011-12-08 21:33 -0500

csiph-web