[Python-Dev] Intricacies of calling __eq__
Mark Shannon
mark at hotpy.org
Wed Mar 19 11:10:42 CET 2014
On 18/03/14 07:52, Maciej Fijalkowski wrote:
> Hi
>
> I have a question about calling __eq__ in some cases.
>
> We're thinking about doing an optimization where say:
>
> if x in d:
> return d[x]
>
> where d is a dict would result in only one dict lookup (the second one
> being constant folded away). The question is whether it's ok to do it,
> despite the fact that it changes the semantics on how many times
> __eq__ is called on x.
Yes it is OK. The number of equality checks is not part of the specification of
the dictionary. In fact, it differs between a 32 and 64 bit build of the same code.
Consider two objects that hash to 2**33+1 and 2**34+1 respectively.
On a 32 bit machine their truncated hashes are both 1, so they must be distinguished
by an equality test. On a 64 bit machine their hashes are distinct and no equality
check is required.
Cheers,
Mark.
More information about the Python-Dev
mailing list