Skip to content

Commit d42086f

Browse files
author
Sakis Kasampalis
committed
Merge branch 'pep8_prototype' of https://github.com/jcdenton/python-patterns into jcdenton-pep8_prototype
2 parents eb455e0 + a444aab commit d42086f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

prototype.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
from copy import deepcopy
1+
import copy
22

33

44
class Prototype:
55
def __init__(self):
6-
self._objs = {}
6+
self._objects = {}
77

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
1311

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]
1715

1816
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))
2119
obj.__dict__.update(attr)
2220
return obj
2321

2422

25-
if __name__ == '__main__':
23+
def main():
2624
class A:
27-
pass
25+
pass
2826

2927
a = A()
3028
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)
3331

3432
print(a)
3533
print(b.a, b.b, b.c)
34+
35+
36+
if __name__ == '__main__':
37+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy