Re: copying classes?
On 30.12.2004, at 01:24, It's me wrote: 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. :=) take a look at the __setstate__ and __getstate__ documentation of copy (e.g. pickle) -- with them you can customize the copying task. Anyway, they work only on instance but not on class objects :( - harold - -- Military intelligence is a contradiction in terms. -- Groucho Marx -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
harold fellermann [EMAIL PROTECTED] wrote: ... I always think that a well designed object should have a copyme method. :=) That would be __copy__ or __deepcopy__, but the __getstate__ / __setstate__ approach is often preferable. take a look at the __setstate__ and __getstate__ documentation of copy (e.g. pickle) -- with them you can customize the copying task. Anyway, they work only on instance but not on class objects :( They should, if you put them in the class of the class -- the metaclass. class mec(type): ... def __copy__(cls): ... return mec('copyof'+cls.__name__, cls.__bases__, dict(vars(cls))) ... class foo: ... __metaclass__ = mec ... bar = copy.copy(foo) bar.__name__ 'copyoffoo' Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
Jeff Epler wrote: Here's an example of attempting to deepcopy a class: class X: pass ... import copy X is copy.deepcopy(X) Traceback (most recent call last): File stdin, line 1, in ? File /usr/lib/python2.2/copy.py, line 179, in deepcopy raise error, \ copy.Error: un-deep-copyable object of type type 'class' Weird. I get (Python 2.3.4): class X: pass ... import copy X is copy.deepcopy(X) True However: class Foo: ... def bar(self, x, y, z): pass ... import copy FooCopy = copy.deepcopy(Foo) FooCopy class __main__.Foo at 0x0142FE70 Foo class __main__.Foo at 0x0142FE70 It appears it doesn't copy the class at all, you just get the same class back. -- Hans Nowak http://zephyrfalcon.org/ -- http://mail.python.org/mailman/listinfo/python-list
copying classes?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
You copied an instance, not a class. Here's an example of attempting to deepcopy a class: class X: pass ... import copy X is copy.deepcopy(X) Traceback (most recent call last): File stdin, line 1, in ? File /usr/lib/python2.2/copy.py, line 179, in deepcopy raise error, \ copy.Error: un-deep-copyable object of type type 'class' In theory, one could provide a metaclass that allows copying of instances of that metaclass. I'll leave this as an exercise to the reader. Jeff pgpjJrXN93ruF.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: copying classes?
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 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list