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


Groups > comp.lang.python > #39221

Re: Making unhashable object

References <20130219143825.3077c3b8@pcolivier.chezmoi.net>
Date 2013-02-20 00:46 +1100
Subject Re: Making unhashable object
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2024.1361281906.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Feb 20, 2013 at 12:38 AM, Olive
<diolu.remove_this_part@bigfoot.com> wrote:
> I am trying to define a class whose instances should not be hashable, following: http://docs.python.org/2/reference/datamodel.html#object.__hash__
>
> class A:
>     def __init__(self,a):
>         self.value=a
>     __hash__=None

This is an old-style class. If you subclass object, it works as you expect:

>>> class A(object):
    def __init__(self,a):
        self.value=a
    __hash__=None

>>> a=A(3)
>>> hash(a)

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    hash(a)
TypeError: unhashable type: 'A'

This is with Python 2.6. With Python 3 and later, that distinction no
longer exists.

ChrisA

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


Thread

Making unhashable object Olive <diolu.remove_this_part@bigfoot.com> - 2013-02-19 14:38 +0100
  Re: Making unhashable object Chris Angelico <rosuav@gmail.com> - 2013-02-20 00:46 +1100
  Re: Making unhashable object Peter Otten <__peter__@web.de> - 2013-02-19 14:54 +0100

csiph-web