fork download
  1. # How to override __setattr__ __getattr__ method
  2. class Overrider(object):
  3. def __init__(self, x):
  4. self.x = x
  5. #print(self.__dict__)
  6.  
  7. def __setattr__(self, key, value):
  8. print("Changing:")
  9. super(Overrider, self).__setattr__(key, value)
  10.  
  11. def __getattribute__(self, key):
  12. #if key.startswith('_'):
  13. # return super(Overrider, self).__getattribute__(key)
  14. return super(Overrider, self).__getattribute__(key)*3
  15.  
  16. obj = Overrider(10)
  17. obj.x = 5
  18. obj.x
Success #stdin #stdout 0.03s 9984KB
stdin
Standard input is empty
stdout
Changing:
Changing: