Skip to content

Commit ca20fdc

Browse files
committed
Simpler and faster prototype
A basic performance test: if __name__ == '__main__': import time for _ in range(10): n = 10000 t0 = time.time() for i in range(n): main() t1 = time.time() total_n = t1 - t0 print(total_n) Results: copy.deepcopy() 0.5343360900878906 0.3827991485595703 0.335590124130249 0.3641550540924072 0.3552978038787842 0.39732789993286133 0.4047999382019043 0.35767602920532227 0.40660595893859863 0.3223540782928467 __class__ 0.056986093521118164 0.04783987998962402 0.04845404624938965 0.043087005615234375 0.04816293716430664 0.051603078842163086 0.046478986740112305 0.044046878814697266 0.048792123794555664 0.05028414726257324
1 parent 2464352 commit ca20fdc

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

creational/prototype.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
import copy
5-
64

75
class Prototype(object):
86

97
value = 'default'
108

119
def clone(self, **attrs):
1210
"""Clone a prototype and update inner attributes dictionary"""
13-
obj = copy.deepcopy(self)
11+
# Python in Practice, Mark Summerfield
12+
obj = self.__class__()
1413
obj.__dict__.update(attrs)
1514
return obj
1615

0 commit comments

Comments
 (0)