[issue21234] __contains__ and friends should check "is" for all elements first

Wolfgang Maier report at bugs.python.org
Tue Apr 15 18:44:55 CEST 2014


Wolfgang Maier added the comment:

>> - if an object is in the container that is equal to the object, it will be slower, but not very much.

You don't know that in general. It depends on where in the sequence the equal object sits, and also on how expensive the equality check is compared to the identity check.

A simplified example over your rather lengthy one:

>>> l=list(range(20000000))
>>> any(e is 9000000 or e == 9000000 for e in l) # mimicking current behaviour
True
>>> any(e is 9000000 for e in l) or any(e == 9000000 for e in l) # your suggestion
True

The second example takes about twice as long as the first one because the identity check has to be run on the whole list, when the equality check discovers the match half way through the sequence.
Given that equality is usually more likely than identity, I guess, you would pay a price for your suggestion more often than you profit from it.

----------
nosy: +wolma

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21234>
_______________________________________


More information about the Python-bugs-list mailing list