git.net

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

keying by identity in dict and set


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!

Thanks!

On Sun, Oct 27, 2019 at 9:04 AM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Sun, Oct 27, 2019 at 6:26 PM Steve White <stevan.white at gmail.com> wrote:
> > As near as I can tell, returning the id() in __hash__() results in a
> > perfect hash key.  I really want to know if that is true.
> > Because if it is true, any further layer is simply covering for a
> > failing in the documentation.
>
> Only if your __eq__() does not return True for anything other than
> itself. That is the entire definition of __hash__ - it needs to return
> the same value for two objects that are equal. This is exactly what
> the documentation said, as multiple people have posted here.
>
> If your object is equal only to itself, then its hash can be its
> identity. Otherwise it should not. It is that simple.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list