Skip to content

Commit ca9e299

Browse files
committed
better prototype example
1 parent 4b81a4a commit ca9e299

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

‎prototype.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ def clone(self, name, **attr):
2323
obj.__dict__.update(attr)
2424
returnobj
2525

26+
classA:
27+
def__init__(self):
28+
self.x=3
29+
self.y=8
30+
self.z=15
31+
self.garbage= [38, 11, 19]
32+
33+
def__str__(self):
34+
return'{}{}{}{}'.format(self.x, self.y, self.z, self.garbage)
2635

27-
defmain():
28-
classA:
29-
pass
3036

37+
defmain():
3138
a=A()
3239
prototype=Prototype()
33-
prototype.register_object('a', a)
34-
b=prototype.clone('a', a=1, b=2, c=3)
35-
36-
print(a)
37-
print(b.a, b.b, b.c)
38-
40+
prototype.register_object('objecta', a)
41+
b=prototype.clone('objecta')
42+
c=prototype.clone('objecta', x=1, y=2, garbage=[88, 1])
43+
print([str(i) foriin (a, b, c)])
3944

4045
if__name__=='__main__':
4146
main()
4247

4348
### OUTPUT ###
44-
# <__main__.main.<locals>.A object at 0x7fc1d23272d0>
45-
# 1 2 3
49+
# ['3 8 15 [38, 11, 19]', '3 8 15 [38, 11, 19]', '1 2 15 [88, 1]']

0 commit comments

Comments
(0)