git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

keying by identity in dict and set


On Mon, Oct 28, 2019 at 7:34 PM Steve White <stevan.white at gmail.com> wrote:
>
> Hi Chris,
>
> I'm afraid you've missed my point.  As I said in the initial post, I
> have read the documentation.
>
> I think the documentation does not adequately explain how the
> hashtable (or hashtables generally) work internally.
>
> I think that in fact, if __hash__() returns a unique integer for each
> key, __eq__() is in fact *never* called.
> There is a reason for this: as I explained, this situation results in
> a "perfect hash", in which no key collision can occur,
> the bucket never needs to be searched.
> The experiment provided in the initial post seems to back this up.
>
> If you can shed light on the internals of dict and set and their
> design goals, please do so!

Yes, eq will never be called, because it is assumed that any objects
that have different hashes must not compare equal. That is the entire
point of the hash. So if your objects might compare equal to each
other, they MUST have the same hash. That is what the documentation
says.

If you don't define EITHER hash OR eq, then you will get a hash based
on the object's identity, and it will be equal only to itself. That is
the default behaviour.

ChrisA