On Oct 15, 12:11 pm, Austin Bingham <austin.bing...@gmail.com> wrote: > To put it in code, I want this: > > s = set(hash_func = lambda obj: hash(obj.name), eq_func = ...) > ... > x.name = 'foo' > y.name = 'foo' > s.add(x) > s.add(y) # no-op because of uniqueness criteria > assert len(s) == 1
I wrote a quick subclass of set that does something similar, but uses just one function for the object uniqueness: class MySet(set): def __init__(self, iterable = (), idfunc = lambda x: x): self.idfunc = idfunc self.ids = set() for item in iterable: self.add(item) def add(self, item): id = self.idfunc(item) if id not in self.ids: self.ids.add(id) set.add(self, item) >>> class Foo(object): ... def __init__(self, name): ... self.name = name ... >>> x = Foo('foo') >>> y = Foo('foo') >>> s = MySet(idfunc = lambda x: x.name) >>> s.add(x) >>> s MySet([<__main__.Foo object at 0x00A89F90>]) >>> s.add(y) >>> s MySet([<__main__.Foo object at 0x00A89F90>]) Is this what you are looking for? -- http://mail.python.org/mailman/listinfo/python-list