[Python-Dev] Intricacies of calling eq (original) (raw)
Mark Shannon mark at hotpy.org
Wed Mar 19 11:10:42 CET 2014
- Previous message: [Python-Dev] Intricacies of calling __eq__
- Next message: [Python-Dev] Intricacies of calling __eq__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 233+1 and 234+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.
- Previous message: [Python-Dev] Intricacies of calling __eq__
- Next message: [Python-Dev] Intricacies of calling __eq__
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]