I am stuck in a problem with delegation in Python. I read a program as following:
class wrapper:
def __init__(self,object):
self.wrapped=object
def __getattr__(self,attrname): #reload the dot-calculation
print('Trace:', attrname) #print the attribute of the object
return getattr(self.wrapped, attrname) #execute the attributes of the object
i=[1,2,3]
myWrapper=wrapper(i)
wrapper.wrapped #execute the dot-calculation
wrapper.append(4) #execute the dot-calculation
In my opinion, both of these two executions of the dot-calculation should print 'Trace'. However, I only get Trace for one time.
wrapper.wrapped #never print 'Trace'
wrapper.append(4) #print ('Trace:', append')
I can't understand this. Is funtion__getattr__ reloaded or not? Why there is nothing printed during the first execution?
Thanks for your explanation!