"a in b" calls a.__cmp__, why?

Terry Reedy tjreedy at udel.edu
Sat Jun 21 22:44:53 CEST 2003

"Gerrit Holl" <gerrit at nl.linux.org> wrote in message
news:mailman.1056224677.5103.python-list at python.org...
> Why does a container-sequence's __contains__ call obj.__cmp__?

Because 'in' is defined by equality, as determined by comparison
returning 0 or not ,  and not by identity.

If you want identity in-ness, either program the search yourself or
use a list  (or dict/set) of ids and search for id(item) in idlist.

If your list is long (enough) and repeatedly searched, sorting and
using binsearch or making a set or dict for hash access will be

Terry J. Reedy

More information about the Python-list mailing list