copying classes?

Bob Van Zant bvanzant at asyncfs.com
Wed Dec 29 19:53:02 CET 2004


Ha. I just read down to the bottom of pyDoc page.

"This version does not copy types like module, class, function, method,
nor stack trace, stack frame, nor file, socket, window, nor array, nor
any similar types."

However, I actually tried it and it worked at least in the simple case:
>>> class x:
...   def __init__(self):
...     self.y = 1
...
>>> obj = x()
>>> obj.y
1
>>>
>>> import copy
>>> z = copy.deepcopy(obj)
>>> z.y
1
>>> obj.y = 4
>>> obj.y
4
>>> z = copy.deepcopy(obj)
>>> z.y
4

-Bob

On Wed, 2004-12-29 at 10:42 -0800, Bob Van Zant wrote:
> copy.deepcopy() should do the trick. This URL answers a little bit of
> your question about the difficulties in copying "complex" data
> structures.
> 
> http://pydoc.org/2.3/copy.html
> 
> -Bob
> 
> On Wed, 2004-12-29 at 19:29 +0100, harold fellermann wrote:
> > Hi all,
> > 
> > In the documentation of module 'copy' it is said that "This version 
> > does not copy types like module, class, function, method, stack trace, 
> > stack frame, file, socket, window, array, or any similar types."
> > 
> > Does anyone know another way to (deep)copy objects of type class? What 
> > is special about the objects of these types that they cannot be easily 
> > copied?
> > 
> > Any help appreciated,
> > 
> > - harold -
> > 
> > 
> > --
> > I like pigs. Dogs look up to us. Cats look down to us.
> > Pigs treat us as equal.
> > -- Winston Churchill
> > 
> 




More information about the Python-list mailing list