[Python-3000] __hash__ : Problem with either documentation or understanding
ncoghlan at gmail.com
Wed Jul 30 00:55:13 CEST 2008
Charles Hixson wrote:
> """If a class defines mutable objects and implements a __cmp__() or __eq__()
> method, it should not implement __hash__(), since the dictionary
> implementation requires that a key’s hash value is immutable (if the object’s
> hash value changes, it will be in the wrong hash bucket).
> What I had been planning to do was implement a hash code created by hashing a
> unique integer, say the sequential order in which the instance of the class
> was created. This would be unique and unchanging number, so the explanation
> of why __hash__ should not be created seems wrong. Or *could* it be created?
> In which case that statement that it shouldn't be created is wrong.
> (Or, of course, I'm misunderstanding things.)
From earlier in the paragraph you quoted:
"""The only required property is that objects which compare equal have
the same hash value;"""
Objects which compare equal must also end up in the same hash bucket in
order for dictionaries to work correctly.
P.S. Chris is right that this question is technically off-topic.
More information about the Python-3000