I'm trying to eliminate duplicates of an array containing a few thousands of custom objects.
For this I'm implementing the __hash__ and __cmp__ functions for these objects.
Is it safe to delegate these functions to one of the objects' fields (a guaranteed unique string) ?
I.e.
class A:
def __init__(self, key, etc...):
self.key = key
...
def __hash__(self):
return self.key.__hash__()
def __cmp__(self, other):
return cmp(self.key, other.key)
# then somewhere in code with arr being array of A
unique = sorted(set(arr))
Any hint greatly appreciated!