__deepcopy__ without recursive copies?

Bernie bday at jvncomm.com
Tue Jan 25 13:16:48 EST 2005


#!/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)




More information about the Python-list mailing list