Rich comparison methods don't work in sets?

Peter Otten __peter__ at
Fri Jun 19 15:37:17 EDT 2009

Gustavo Narea wrote:

> I've noticed that if I have a class with so-called "rich comparison"
> methods
> (__eq__, __ne__, etc.), when its instances are included in a set,
> set.__contains__/__eq__ won't call the .__eq__ method of the elements
> and thus
> the code below:
> """
> obj1 = RichComparisonClass()

How is RichComparisonClass implemented? Did you provide a __hash__() method 
such that obj1 == obj2 implies hash(obj1) == hash(obj2)? This is necessary 
for instances of the class to work properly with sets and dicts.


More information about the Python-list mailing list