Semi-newbie, rolling my own __deepcopy__
Michael Spencer
mahs at telcopartners.com
Mon Apr 4 12:44:07 EDT 2005
ladasky at my-deja.com wrote:
> Hi, folks,
>
> First, the obligatory cheerleading -- then, my questions...
>
> I love Python! I am only an occasional programmer. Still, the logic
> of the language is clear enough that I can retain pretty much all that
> I have learned from one infrequent programming session to the next.
> That's quite an accomplishment for a language this powerful. Also, I'm
> finally beginning to grasp OOP. I could never quite get the hang of it
> in C++ or Java. Recently, I discovered __getitem__ and pickle. Oh,
> yeah.
>
> Anyway, my present problem is that I want to make copies of instances
> of my own custom classes. I'm having a little trouble understanding
> the process. Not that I think that it matters -- but in case it does,
> I'll tell you that I'm running Python 2.3.4 on a Win32 machine.
>
> I started naively, thinking that I could just call copy.deepcopy() and
> be done with it. After getting a TypeError from the interpreter, I
> read the deepcopy docs and discovered that I need to implement a
> __deepcopy__ method in my class. But the docs are a bit vague here.
> What exactly should this __deepcopy__ do? I tried looking for examples
> of __deepcopy__ code on the Net, but I'm not quite understanding what
> I'm finding there. I guess that I'm getting deeper into the guts of
> Python than I planned.
>
> AFAIK, I'm supposed to add a "def __deepcopy__(self, memo):" to my
> class definition. This will get called when I invoke
> copy.deepcopy(myObject). The object to be copied is self, I presume.
> What exactly is memo? The docs say that it's a dictionary which "keeps
> track of what has already been copied." Somewhere I remember reading
> that the namespace of an object is a dictionary. So is memo identical
> to the dictionary of the new object that I'm trying to create? What
> exactly do I add to memo? I think that I should make shallow copies of
> methods, but deep copies of data structures (the contents of which I'm
> likely to change). Do I iterate through and copy the items in
> dir(self)? Do I update memo manually, or does passing memo into copy()
> or deepcopy() automatically update memo's contents? Are there any
> items that I *shouldn't* copy from self to memo? Should __deepcopy__
> return memo?
>
> Sorry for all the confusion -- and thanks for your help!
>
> --
> Rainforest laid low.
> "Wake up and smell the ozone,"
> Says man with chainsaw.
> John J. Ladasky Jr., Ph.D.
>
If you google for:
python __deepcopy__ cookbook
you will find a couple of examples of this method in use, among them:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259179
class deque(object):
def __init__(self, iterable=()):
if not hasattr(self, 'data'):
self.left = self.right = 0
self.data = {}
self.extend(iterable)
[...snip methods...]
def __deepcopy__(self, memo={}):
from copy import deepcopy
result = self.__class__()
memo[id(self)] = result
result.__init__(deepcopy(tuple(self), memo))
return result
HTH
Michael
More information about the Python-list
mailing list