[New-bugs-announce] [issue11988] special method lookup docs don't address some important details

R. David Murray report at bugs.python.org
Tue May 3 22:47:11 CEST 2011


New submission from R. David Murray <rdmurray at bitdance.com>:

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:

http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes

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?

----------
assignee: docs at python
components: Documentation
messages: 135068
nosy: docs at python, r.david.murray
priority: normal
severity: normal
stage: needs patch
status: open
title: special method lookup docs don't address some important details
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3

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


More information about the New-bugs-announce mailing list