Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame^] | 1 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 2 | class Delegator: |
| 3 | |
| 4 | # The cache is only used to be able to change delegates! |
| 5 | |
| 6 | def __init__(self, delegate=None): |
| 7 | self.delegate = delegate |
| 8 | self.__cache = {} |
| 9 | |
| 10 | def __getattr__(self, name): |
| 11 | attr = getattr(self.delegate, name) # May raise AttributeError |
| 12 | setattr(self, name, attr) |
| 13 | self.__cache[name] = attr |
| 14 | return attr |
| 15 | |
| 16 | def resetcache(self): |
| 17 | for key in self.__cache.keys(): |
| 18 | try: |
| 19 | delattr(self, key) |
| 20 | except AttributeError: |
| 21 | pass |
| 22 | self.__cache.clear() |
| 23 | |
| 24 | def cachereport(self): |
| 25 | keys = self.__cache.keys() |
| 26 | keys.sort() |
| 27 | print keys |
| 28 | |
| 29 | def setdelegate(self, delegate): |
| 30 | self.resetcache() |
| 31 | self.delegate = delegate |
| 32 | |
| 33 | def getdelegate(self): |
| 34 | return self.delegate |