[Python-3000] Need help completing ABC pep (original) (raw)

Guido van Rossum guido at python.org
Fri Apr 20 20:52:55 CEST 2007


On 4/20/07, Nick Coghlan <ncoghlan at gmail.com> wrote:

It's not listed as an open issue, but when looking at the list of ABCs, I noticed a potential solution to a different problem we have occasionally wrestled with: the unfortunate side-effects of having a default hash method defined on object.

If the details of collections.Hashable.hash are declared to be implementation dependent rather than having it always return 0, then we can get rid of object.hash. For CPython, collections.Hashable.hash would then just invoke id() on the object, and other implementations can also do whatever they currently do for object.hash

I don't like that at all. I want to force folks to implement a hash that matches their eq. As soon as you implement an eq you need to redefine hash. I'd much rather have the default hash be correct but insanely slow than fast but incorrect -- it being incorrect is much harder to debug, and it smells of premature optimization. As Hashable.hash is abstract, you are forced to define your own hash.

I do see your point though: object.hash makes it possible to use all sorts of objects as dict keys by default, as long as they don't implement eq. But perhaps this is not such a good idea and we should just get rid of it and add hash back to specific objects that are useful to use as keys? (E.g. classes but not iterators, to take some extreme examples.)

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-3000 mailing list