[New-bugs-announce] [issue2235] __eq__ / __hash__ check doesn't take inheritance into account

jason kirtland report at bugs.python.org
Tue Mar 4 20:49:24 CET 2008


New submission from jason kirtland:

In 2.6a, seems like the __hash__ implementation and __eq__ must be
defined together, in the same class.  See also #1549.  Ensuring that a
__hash__ implementation isn't being pulled from a builtin type is
probably a sufficient check...?

>>> class Base(object):
...     def __init__(self, name):
...         self.name = name
...     def __eq__(self, other):
...         return self.name == other.name
...     def __hash__(self):
...         return hash(self.name)
... 
>>> class Extended(Base):
...     def __eq__(self, other):
...         print "trace: __eq__ called"
...         return Base.__eq__(self, other)
... 
>>> hash(Base('b1'))
603887253
>>> hash(Extended('e1'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'Extended'

----------
components: Interpreter Core
messages: 63258
nosy: jek
severity: normal
status: open
title: __eq__ / __hash__ check doesn't take inheritance into account
type: crash
versions: Python 2.6

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2235>
__________________________________


More information about the New-bugs-announce mailing list