-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.py
More file actions
37 lines (25 loc) · 879 Bytes
/
adapter.py
File metadata and controls
37 lines (25 loc) · 879 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
""" Adapter is used to generalize the function name in different classes """
class Russian:
def __init__(self) -> None:
self.name = 'Russian Language'
def speak_russian(self) -> str:
return 'Privet'
class English:
def __init__(self) -> None:
self.name = 'English Language'
def speak_english(self) -> str:
return 'Hello'
class Adapter:
def __init__(self, obj, **adapted_method) -> None:
self._object = obj
self.__dict__.update(adapted_method)
def __getattr__(self, attr):
return getattr(self._object, attr)
russian = Russian()
english = English()
objects = [
Adapter(russian, speak=russian.speak_russian),
Adapter(english, speak=english.speak_english)
]
for item in objects:
print('{} says {}'.format(item.name, item.speak()))