set using alternative hash function?

Diez B. Roggisch deets at
Thu Oct 15 15:43:00 CEST 2009

>> And if there were something that would decide on context which of several
>> implementations to use, you'd have less to worry. As things are, there
>> isn't such thing (I don't even have the slightest idea what could work),
>> you are as well off with defining two functions.
> But this "context decider" you're talking about sounds exactly like
> what I'm talking about.  If I had some class with __hash1__ and
> __hash2__ (and associated equality operators), you're saying it would
> be nice to be able to select which to use based on the context (e.g.
> what type of set I'm using.) It might look like this:
>    s = set(hash_func = lambda x: x.__hash2__, eq_func = lambda x, y:
> x.__eq2__(y))
> And if *that* works for you, do you still have a problem with:
>   s = set(hash_func = lambda x: hash(, eq_func = lambda x,y:
> ==
> ?
> If you don't like those, what would work for you? As far as I can
> tell, your "context decider" and my "alternative hash functions" are
> the same thing, and we just need to agree on a syntax.

The context-decider isn't the same thing because it isn't designed yet :)
And most probably won't ever be. It's just the abstract idea that
hashing/equality change for one object depending on the circumstances they
are used in, and that one wishes that the decision what to use is as simple
& transparent as possible. 

Your approach is certainly viable, but I guess the current
set-implementation is optimized on working with __hash__ and __eq__ on
objects because for these exist slots in the python object structure in C.
So while you can implement your idea, you will end up passing wrappers as
Chris & me proposed into the current set implementation. 

However, it might be worth thinking of proposing this change to the set-type
in general. But then for orthogonality, dicts should have it as well I
guess. Which opens a whole new can of worms.

I'm undecided on this. I think the cure is straight forward & simple to
implement, and once you have the level to understand & need different
hashing/equality, you should be comfortable writing this simple wrapper
yourself. So for me personally, it's not a needed addition to the language.
YMMV, and thus - go for it if you like.


More information about the Python-list mailing list