|
1 | | -from copy import deepcopy |
| 1 | +import copy |
2 | 2 |
|
3 | 3 |
|
4 | 4 | class Prototype: |
5 | 5 | def __init__(self): |
6 | | - self._objs = {} |
| 6 | + self._objects = {} |
7 | 7 |
|
8 | | - def registerObject(self, name, obj): |
9 | | - """ |
10 | | - register an object. |
11 | | - """ |
12 | | - self._objs[name] = obj |
| 8 | + def register_object(self, name, obj): |
| 9 | + """Register an object""" |
| 10 | + self._objects[name] = obj |
13 | 11 |
|
14 | | - def unregisterObject(self, name): |
15 | | - """unregister an object""" |
16 | | - del self._objs[name] |
| 12 | + def unregister_object(self, name): |
| 13 | + """Unregister an object""" |
| 14 | + del self._objects[name] |
17 | 15 |
|
18 | 16 | def clone(self, name, **attr): |
19 | | - """clone a registered object and add/replace attr""" |
20 | | - obj = deepcopy(self._objs[name]) |
| 17 | + """Clone a registered object and update inner attributes dictionary""" |
| 18 | + obj = copy.deepcopy(self._objects.get(name)) |
21 | 19 | obj.__dict__.update(attr) |
22 | 20 | return obj |
23 | 21 |
|
24 | 22 |
|
25 | | -if __name__ == '__main__': |
| 23 | +def main(): |
26 | 24 | class A: |
27 | | - pass |
| 25 | + pass |
28 | 26 |
|
29 | 27 | a = A() |
30 | 28 | prototype = Prototype() |
31 | | - prototype.registerObject("a", a) |
32 | | - b = prototype.clone("a", a=1, b=2, c=3) |
| 29 | + prototype.register_object('a', a) |
| 30 | + b = prototype.clone('a', a=1, b=2, c=3) |
33 | 31 |
|
34 | 32 | print(a) |
35 | 33 | print(b.a, b.b, b.c) |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == '__main__': |
| 37 | + main() |
0 commit comments