Saturday, 7 September 2013

Comparison of python objects wrt an attribute

Comparison of python objects wrt an attribute

I have a tuple of python objects, from which I need a list of objects with
no duplicates, using set() (this check for duplicate objects is to be done
on an attribute.). This code will give a simple illustration:
class test:
def __init__(self, t):
self.t = t
def __repr__(self):
return repr(self.t)
def __hash__(self):
return self.t
l = (test(1), test(2), test(-1), test(1), test(3), test(2))
print l
print set(l)
However, it did not work. I can do it on an iteration over l, but any idea
why set() is not working? Here is the official documentation.

No comments:

Post a Comment