Delegator.py 665 B

12345678910111213141516171819202122232425
  1. class Delegator:
  2. # The cache is only used to be able to change delegates!
  3. def __init__(self, delegate=None):
  4. self.delegate = delegate
  5. self.__cache = set()
  6. def __getattr__(self, name):
  7. attr = getattr(self.delegate, name) # May raise AttributeError
  8. setattr(self, name, attr)
  9. self.__cache.add(name)
  10. return attr
  11. def resetcache(self):
  12. for key in self.__cache:
  13. try:
  14. delattr(self, key)
  15. except AttributeError:
  16. pass
  17. self.__cache.clear()
  18. def setdelegate(self, delegate):
  19. self.resetcache()
  20. self.delegate = delegate