![python __getattr__ __setattr__ python __getattr__ __setattr__](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
class Rectangle:
def __init__(self):
self.width = 0
self.height = 0 def __setattr__(self, key, value):
if key == 'size':
self.width, self.height = value
else:
self.__dict__[key] = value def __getattr__(self, item):
if item == 'size':
return self.width, self.height
else:
return self.__dict__[item] tR = Rectangle()
tR.__setattr__('size',(1,3))
print(tR.__getattr__('size'))
resule:
(1, 3)