Terry J. Reedy <tjre...@udel.edu> added the comment:

In 3.1, Section 5.9 has
"
For user-defined classes which define the __contains__() method, x in y
is true if and only if y.__contains__(x) is true.

For user-defined classes which do not define __contains__() and do
define __getitem__(), x in y is true if and only if there is a
non-negative integer index i such that x == y[i], and all lower integer
indices do not raise IndexError exception. (If any other exception is
raised, it is as if in raised that exception).
"
However, discussion of how user-defined classes emulate builtins is
mostly in 3.3. Special method names. I think some of the above should be
moved, with or without revision, (or copied) to 3.3.5. Emulating
container types.

The current entry there says only 
"
object.__contains__(self, item) 
Called to implement membership test operators. Should return true if
item is in self, false otherwise. For mapping objects, this should
consider the keys of the mapping rather than the values or the key-item
pairs.
"
which seems to me inadequate, as it does not discuss the alternative, as
many other entries in 3.3 do.

Regardless of that, I verified that in 3.1, __iter__ is called in
preference to __getitem__:

class C():
    def __iter__(s):
        print('in iter')
        return iter([])
    def __getitem(s,i):
        print('in getitem')

print(1 in C())

prints
in iter
False

so some change is needed.

----------
nosy: +tjreedy

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue6324>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to