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

Raymond Hettinger report at bugs.python.org
Thu Apr 17 03:03:51 CEST 2014


Raymond Hettinger added the comment:

>  when doing an operation that does linear search through a container,
> the interpreter checks all items one by one, first checking identity
> and then equality.

This is the guaranteed behavior.  Changing it would result in subtle change in semantics (i.e. an equality match early in a sequence would be bypassed in favor of a later identity match).

The current behavior also has favorable cache characteristics (i.e. each element is accessed exactly once) which provide benefits for long lists that cannot fit in L1 or L2 cache.

That said, I feel your pain.  Slow __eq__ tests are the bane of linear search.  I recommend using a hashed container for fast searching or that you use a tool like PyPy that gives significant speed-ups.

----------
resolution:  -> rejected
status: open -> closed
versions:  -Python 3.1, Python 3.2, Python 3.3, Python 3.4

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


More information about the Python-bugs-list mailing list