467,223 Members | 1,400 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,223 developers. It's quick & easy.

__deepcopy__ without recursive copies?

#!/usr/bin/env python
import sys
import copy

'''
How to define __deepcopy__ with out causing recursive calls to copies
of self?

Bernie Day 01/25/05

I was using deepcopy on DeviceManager and this worked well. When I
defined
__deepcopy__, so I could have a handle to the children of the Master
device Manager
I got into trouble.
I see that there is a method using a dic to limit this, but I can't
seem to get it to work.

This is not the code I was using but should represent it well.

Thanks!
'''
class DeviceManager:
def __init__(self,devFile):
DevFile = open(devFile)
devList = [Device(line) for line in DevFile]
#etc, etc...
def __deepcopy__(self):
miniMe = copy.deepcopy(self)
miniMe.devList = tuple(devList)
return miniMe
class Device:
def __init__(self,line):
self.copyies = []
#do something with line here
def __deepcopy__(self):
miniMe = copy.deepcopy(self)
self.copyies.append(miniMe)
return miniMe

DevMan1 = DeviceManager(devfile)
devMan2 = copy.deepcopy(DevMan1)

Jul 18 '05 #1
  • viewed: 1857
Share:
3 Replies
Here I found something on __deepcopy__
http://groups-beta.google.com/group/...7866340239edd7

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

Jul 18 '05 #2
In this thread a solution is given on how to work with __deepcopy__:
http://groups-beta.google.com/group/...7866340239edd7

For you this would give 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

Jul 18 '05 #3


That is clever, gives a lot of insight into how the __dict__ == the
object.
This is somewhat like the solution I am using from the Cookbook, an
Empty object copy. This is cleaner and very much more concise.
Thank you!

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by windandwaves | last post: by
10 posts views Thread by zahy[dot]bnaya[At]gmail[dot]com | last post: by
10 posts views Thread by JurgenvonOerthel@hotmail.com | last post: by
2 posts views Thread by aaragon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.