Assuming the duplicate A(2) is a typo, I just need to define __hash__ (see docs ):
import numpy as np from functools import total_ordering @total_ordering class A(object): def __init__(self, a): self.a = a def __lt__(self, other): return self.a < other.a def __eq__(self, other): return self.a == other.a def __ne__(self, other): return self.a != other.a def __hash__(self): return hash(self.a) def __repr__(self): return "A({})".format(self.a) def __str__(self): return repr(self)
produces
>>> map(A, range(3)+range(3)) [A(0), A(1), A(2), A(0), A(1), A(2)] >>> set(map(A, range(3)+range(3))) set([A(0), A(1), A(2)]) >>> np.unique(map(A, range(3)+range(3))) array([A(0), A(1), A(2)], dtype=object)
where I used total_ordering to reduce the distribution of methods, as you suggested, it was possible .: ^)
[Edited after publication to fix missing __ne__ .]
source share