class A(object):
def __init__(self):
self.x = 5
def __getattr__(self, name):
print('A.__getattr__(', self, ',', name, ')')
setattr(self, name, 10)
return 10
def __setattr__(self, name, value):
print('A.__setattr__(', self, ',', name, ',', value, ')')
self.__dict__[name] = value
a = A()
print('P1:', a.x)
print('P2:', a.x)
a.x = 15
print('P3:', a.x)
print('P4:', a.y)
print('P5:', a.y)
a.y = 20
print('P6:', a.y)
Item 22: getattr and setattr
Posted by Logan Chien