I have a collection of objects, and I'm interested in getting a specific object from the collection. After some research, I decided to use the solution provided here: http://code.activestate.com/recipes/499299/
The problem is that it does not work.
I have two classes defined as such:
class Foo(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c def __key(self): return (self.a, self.b, self.c) def __eq__(self, other): return self.__key() == other.__key() def __hash__(self): return hash(self.__key()) class Bar(Foo): def __init__(self, a, b, c, d, e): self.a = a self.b = b self.c = c self.d = d self.e = e
Note: the equality of these two classes should be defined only for attributes a, b, c.
The _CaptureEq at http://code.activestate.com/recipes/499299/ also defines its own __eq__ method. The problem is that this method is never called (I think). Consider
bar_1 = Bar(1,2,3,4,5) bar_2 = Bar(1,2,3,10,11) summary = set((bar_1,)) assert(bar_1 == bar_2) bar_equiv = get_equivalent(summary, bar_2)
bar_equiv.d should be 4, and bar_equiv .e should be 5, but it is not. As I mentioned, it looks like the __CaptureEq __eq__ method is not called when the bar_2 in summary statement is bar_2 in summary .
Is there a reason why the __CaptureEq __eq__ method is not called? Hope this is not too obscure to the issue.