set using alternative hash function?

Austin Bingham austin.bingham at gmail.com
Fri Oct 16 00:31:27 EDT 2009


On Thu, Oct 15, 2009 at 7:05 PM, Anthony Tolle <anthony.tolle at gmail.com> wrote:
> 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)

Yes, what you've got there provides the interface of what I want. And
no doubt we could concoct countless ways to implement some version of
this. However, if set itself accepted an alternate hash function, then
I could do it with no extra overhead. Consider your implementation: it
requires an extra set (extra space) and an extra lookup on many
operations (extra time.) My original hope was to not have to do that.

Austin



More information about the Python-list mailing list