I want to redefine an object's get function so that I can intercept when any attribute is requested. Using the getattr function, it doesn't catch when existing variables (eg. attr1 in this case) are requested. Is there a way around this, so I can run a function when attr1 is requested?
class Test(object):
attr1 = 1
def __init__(self):
self.attr2 = 1
def __getattr__(self, attr):
print 'GETATTR', attr
a = Test()
a.attr1
a.attr2
a.attr3
Output:
GETATTR attr3
I'd like to see GETATTR attr1 and GETATTR attr2 in the output too.