Recursion limit problems

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 14 15:03:56 EDT 2007


En Mon, 14 May 2007 12:35:16 -0300, elventear <elventear at gmail.com>  
escribió:

> Since I am defining a hash for my object, it makes sense that I should
> be able to define equality. But I am not sure about inequality, in my
> specific case. The paragraph above mentions that __cmp__ should be
> defined if I define a __hash__, but in the default behaviour of
> __cmp__ inequality is included. So what should I do? Also why is
> equality necessary to be defined? Do dicts only use __hash__ or do
> they use equality as well?

Dicts and sets use the key's hash value to determine the "bucket" where  
the key will be placed, and == to distingish between different objects  
with the same hash value.
That is, you should define __hash__ and one of (__cmp__ or __eq__).  
__neq__ (inequality) isn't required nor used by dict/set implementation.  
(Anyway, Python will transform a!=b into not(a==b), if __neq__ isn't  
defined). Neither <, <=, >, >= are used.
The important thing is that, if two objects compare equal, they must have  
the same hash value. That is: (a==b) => (hash(a)==hash(b)) (the reciprocal  
is not true).

-- 
Gabriel Genellina




More information about the Python-list mailing list