This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients afoglia, georg.brandl, rhettinger, terry.reedy
Date 2009-06-26.21:25:55
SpamBayes Score 7.2164497e-16
Marked as misclassified No
Message-id <1246051558.44.0.681793095281.issue6324@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2009-06-26 21:25:58terry.reedysetrecipients: + terry.reedy, georg.brandl, rhettinger, afoglia
2009-06-26 21:25:58terry.reedysetmessageid: <1246051558.44.0.681793095281.issue6324@psf.upfronthosting.co.za>
2009-06-26 21:25:57terry.reedylinkissue6324 messages
2009-06-26 21:25:55terry.reedycreate