[Python-Dev] getting rid of default object.hash (SF 660098) (original) (raw)

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


[Barry]

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

[Guido]

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) 45632543 hash(break) Traceback (most recent call last): NotImplementedError: Must explicitly define hash for non-default comparisons

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.

Cheers, Nick.

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



More information about the Python-Dev mailing list