__deepcopy__ without recursive copies?

wittempj at hotmail.com wittempj at hotmail.com
Tue Jan 25 15:34:33 EST 2005


Here I found something on __deepcopy__
http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/c34431c5fd3c223b/427866340239edd7?q=__deepcopy__+example&_done=%2Fgroups%3Fq%3D__deepcopy__+example+%26hl%3Den%26lr%3D%26safe%3Doff%26sa%3DN%26tab%3Dwg%26&_doneTitle=Back+to+Search&&d#427866340239edd7

if applied on your script it gives something like:
-import copy
-class DeviceManager(object):
-    def __init__(self,devFile):
-        DevFile = open(devFile)
-        devList = [Device(line) for line in DevFile]
-        #etc, etc...
-
-    def __deepcopy__(self, memo):
-        x = DeviceManager.__new__(DeviceManager)
-        memo[id(self)] = x
-        for n, v in self.__dict__.iteritems():
-            setattr(x, n, copy.deepcopy(v, memo))
-        return x
-
-class Device(object):
-    def __init__(self,line):
-        self.copies = []
-        #do something with line here
-
-    def __deepcopy__(self, memo):
-        x = Device.__new__(Device)
-        memo[id(self)] = x
-        for n, v in self.__dict__.iteritems():
-            setattr(x, n, copy.deepcopy(v, memo))
-        return x
-
-DevMan1 = DeviceManager(r'c:\autoexec.bat')
-DevMan2 = copy.deepcopy(DevMan1)
-
-print DevMan1 is DevMan2
it prints False, so the deepcopy seems to work




More information about the Python-list mailing list