I have this code:
class A:
def __init__(self):
def method(self, item):
print self, ": Getting item", item
self.__getitem__ = types.MethodType(method, self, self.__class__)
class B(object):
def __init__(self):
def method(self, item):
print self, ": Getting item", item
self.__getitem__ = types.MethodType(method, self, self.__class__)
Then this works fine:
a = A()
a[0]
But this does not:
b = B()
b[0]
raising TypeError.
I found that new-style classes look for magic methods in class __dict__ instead of instance __dict__ . Is this right? Why is it so? Do you know about any article explaining the ideas behind? I tried RTFM, but maybe not the right ones or did not catch the thing...
Thank you very much!
Paul
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…