[Python-Dev] getting rid of default object.__hash__ (SF 660098)

Nick Coghlan ncoghlan at iinet.net.au
Tue Dec 23 05:03:55 EST 2003

>>Would it be better if object.__hash__() raised a NotImplementedError?

> It can't -- it's type.__hash__(object).

If I understand Barry's suggestion correctly, he means to keep 
object.__hash__, but have it raise a specific, meaningful error instead 
of making a potentially incorrect assumption as it does now.

E.g. (not real code, since this would behave differently in Py 2.3.3)

 >>> class Works(object): pass
 >>> class Breaks(object):
...   def __cmp__(): return 0 # All instances are created equal!
 >>> work = Works()
 >>> break = Breaks()
 >>> hash(work)
 >>> hash(break)
Traceback (most recent call last):
      <Traceback info>
   NotImplementedError:  Must explicitly define __hash__ for non-default 

hash(work) is fine, since there is no __cmp__ or __eq__ override in 
Works, and hence object.__hash__ never gets called.

hash(break) raises the exception because of the existence of the (rather 
useless) __cmp__ function in the Breaks class.


Nick Coghlan               |     Brisbane, Australia
Email: ncoghlan at email.com  | Mobile: +61 409 573 268

More information about the Python-Dev mailing list