Author r.david.murray
Recipients docs@python, r.david.murray
Date 2011-05-03.20:47:09
SpamBayes Score 2.13339e-06
Marked as misclassified No
Message-id <>
The following code:

class X(list):

    def __contains__(self, key):
        print('X contains:', key)

class Y():

    def __init__(self, x):
        self.x = x

    def __getattr__(self, key):
        return getattr(self.x, key)

    def __iter__(self):
        print('Y iter')
        return iter([1,2])

x = X()
y = Y(x)

print('res:', 1 in y)

prints True.  It has been explained to me that this is because of:

However, there is no way in the world that I would guess the behavior above from the documentation provided (and I find it surprising...I expected x's __contains__ to get called because Y (a class, not an instance) doesn't have a __contains__ method).

Can anyone explain it more clearly and update the documentation?
Date User Action Args
2011-05-03 20:47:11r.david.murraysetrecipients: + r.david.murray, docs@python
2011-05-03 20:47:11r.david.murraysetmessageid: <>
2011-05-03 20:47:09r.david.murraylinkissue11988 messages
2011-05-03 20:47:09r.david.murraycreate