blob: 3665247c97d5c294fcb1fc5a0978868678a4a332 [file] [log] [blame]
Guido van Rossum504b0bf1999-01-02 21:28:54 +00001
Guido van Rossum3b4ca0d1998-10-10 18:48:31 +00002class 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