[Python-bugs-list] [ python-Bugs-427698 ] objects with __eq__ are not hashable

noreply@sourceforge.net noreply@sourceforge.net
Sun, 27 May 2001 14:26:18 -0700


Bugs item #427698, was updated on 2001-05-27 05:35
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=427698&group_id=5470

>Category: Documentation
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
>Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: objects with __eq__ are not hashable

Initial Comment:
The following code fails with Python2.1, on both SGI
IRIX 6.5 and Linux (Suse7.1)

class A:
    _name = 'AA'
    def __init__(self,name):
        self._name = name
    def __eq__(self,a):
        if (a._name == self._name):
            return 1
        else:
            return 0
    def __str__(self):
        return self._name

a = A('abc')

c = {}

c[a] = 1


===> 
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unhashable instance

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-05-27 14:26

Message:
Logged In: YES 
user_id=31435

If a class defines __eq__ or __cmp__, then for instances to 
be hashable it must also define __hash__.

Changed Category to Documentation and assigned to Fred.  
Ref Man Section 3.3.1 (Basic customization) explains this 
about __hash__, but only mentions __cmp__.  Guido should 
have changed this to include __eq__ too (i.e., everywhere 
__hash__ mentions __cmp__() now it should add "or __eq__
()").

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=427698&group_id=5470