Checking membership in python list trouble -
i encountered problem item membership in list, can't understand. while checking object instance in list( simple "item in list" ) returns me false if instance in list. notice same pointers have no same id.
do have use special method or what? can helps me compare instance equality, please.
note: occasion happens in pyside if matters.
your objects need have __eq__
method define how compare objects equality.
in [18]: class a(object): ...: def __init__(self, n): ...: self.n = n ...: in [19]: class b(object): ...: def __init__(self, n): ...: self.n = n ...: def __eq__(self, other): ...: return self.n == other ...: in [20]: = a(1) in [21]: b = b(1) in [22]: in [a(n) n in range(10)] out[22]: false in [23]: b in [b(n) n in range(10)] out[23]: true in [24]: b in [b(n) n in range(10, 20)] out[24]: false
Comments
Post a Comment