New submission from R. David Murray <[email protected]>:
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@python
components: Documentation
messages: 135068
nosy: docs@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 <[email protected]>
<http://bugs.python.org/issue11988>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com