[Python-Dev] Intricacies of calling eq (original) (raw)

Terry Reedy tjreedy at udel.edu
Tue Mar 18 10:05:56 CET 2014


On 3/18/2014 3:52 AM, 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]

if d.contains(x): return d.getitem(x)

I do not see any requirement to call x.eq any particular number of times. The implementation of d might always call somekey.eq(x). The concept of sets (and dicts) requires coherent equality comparisons.

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.

A eq that has side-effects violates the intended and expected semanitics of eq.

-- Terry Jan Reedy



More information about the Python-Dev mailing list