i had to implement this today, this is what the class looks like
class Foo(object):
def __init__(self, val):
self.Value = val
def __eq__(self, other):
return self.Value == other.Value
foos = [Foo(1), Foo(2)]
xfoo = Foo(2)
print xfoo in foos # this calls the __eq__ function, will return True
xfoo = Foo(4)
print xfoo in foos # will return False
when we implement __eq__ [equal], we should also implement __ne__ [not equal] for != or not in operators.
Comment:
i had to implement this today, this is what the class looks like class Foo(object): def __init__(self, val): self.Value = val def __eq__(self, other): return self.Value == other.Value foos = [Foo(1), Foo(2)] xfoo = Foo(2) print xfoo in foos # this calls the __eq__ function, will return True xfoo = Foo(4) print xfoo in foos # will return False when we implement __eq__ [equal], we should also implement __ne__ [not equal] for != or not in operators.
Posted by infogami