-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.py
More file actions
48 lines (35 loc) · 985 Bytes
/
adapter.py
File metadata and controls
48 lines (35 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class A:
_name="A"
def call_A(self):
print "You have called A!"
class B:
def call_B(self):
print "You have called B!"
class C:
def call_C(self):
print "You have called C!"
class Adapter(object):
_initialised=False #private class variable
def __init__(self, obj, **adapted_methods):
self.obj=obj
for key, value in adapted_methods.items():
function = getattr(self.obj,value)
self.__setattr__(key,function)
self._initialised=True
def __getattr__(self,attr):
return getattr(self.obj,attr)
def __setattr__(self,key,value):
if not self._initialised:
super(Adapter,self).__setattr__(key,value)
else:
setattr(self.obj,key,value)
if __name__ == '__main__':
adapters=[Adapter(A(),call='call_A'),Adapter(B(),call='call_B'),Adapter(C(),call='call_C')]
for adapter in adapters:
adapter.call()
A_adapter = adapters[0]
print A_adapter._name
print A_adapter.obj._name
A_adapter._name="New A"
print A_adapter._name
print A_adapter.obj._name