Hello, everyone. 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() obj2 = RichComparisonClass()
set1 = set([obj1]) set2 = set([obj2]) if obj1 == obj2: print "Objects 1 and 2 are equivalent" else: print "Objects 1 and 2 aren't equivalent" if set1 == set2: print "Sets 1 and 2 are equivalent" else: print "Sets 1 and 2 aren't equivalent" """ Would output: """ Objects 1 and 2 are equivalent Sets 1 and 2 aren't equivalent """ instead of: """ Objects 1 and 2 are equivalent Sets 1 and 2 are equivalent """ How can I work around this? The only solution that comes to my mind is to write a function like this: """ def same_sets(set1, set2): if len(set1) != len(set2): return False for element1 in set1: found = False for element2 in set2: if element1 == element2: found = True break; if not found: return False return True """ But I see it pretty ugly; also I'd also have to roll out my own "contains" (e.g., `element in set`) function. I expect and hope there's a pythonic way to do this. Does it exist? Thanks in advance! -- http://mail.python.org/mailman/listinfo/python-list