Message135068
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? |
|
Date |
User |
Action |
Args |
2011-05-03 20:47:11 | r.david.murray | set | recipients:
+ r.david.murray, docs@python |
2011-05-03 20:47:11 | r.david.murray | set | messageid: <1304455631.01.0.264040156642.issue11988@psf.upfronthosting.co.za> |
2011-05-03 20:47:09 | r.david.murray | link | issue11988 messages |
2011-05-03 20:47:09 | r.david.murray | create | |
|