copying classes?

It's me itsme at yahoo.com
Thu Dec 30 01:24:11 CET 2004


I would not think that a generic deepcopy would work for all cases.   An
object can be as simple as a number, for instance, but can also be as
complex as the universe.  I can't imagine anybody would know how to copy a
complex object otherthen the object itself.

I always think that a well designed object should have a copyme method.
:=)

"Bob Van Zant" <bvanzant at asyncfs.com> wrote in message
news:mailman.8563.1104346385.5135.python-list at python.org...
> 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