Saturday, July 08, 2006

Python note: __hash__

__hash__ provides the hash code for an object. It is used under two circumstances: when the object is put in a dictionary; or when hash() built-in function is called.

There is a requirement for this method - two objects should have the same hash when their __cmp__ returns zero (they are equal). Thus when a class does not provide __cmp__ member function, it should not provide __hash__.

No comments: