Method returning new instance of class?

Arthur ajsiegel at optonline.com
Tue Sep 7 15:53:01 CEST 2004


"Martin v. Löwis" <martin at v.loewis.de> wrote in message news:<4139E730.4060203 at v.loewis.de>...
> Arthur wrote:
> > The app is graphical, and I use a Python extensions in C++ using the Boost
> > library (vpython, new version).  My class instance has an attribute which is
> > a vpython object. Copy.copy doesn't get me where I need to be because my new
> > instance gets a reference to the same vpython object, and changes to it are
> > reflected in the original instance. 
>  [...]
> > 
> > But do you see any reason why this might be?
> 
> Certainly. copy.copy expects that each object follows a certain protocol
> for copying. Copying of certain types (including all classic classes)
> is build into copy.py. For newstyle classes and all other types, copying
> procedures must be registered with copy_reg. If a type is not registered
> with copy_reg, as a last fall back, the __reduce_ex__ and __reduce__
> functions are invoked for the type. If not specifically overridden, they
> always return the original object.


Back at a high bandwidth connection I decided to try to do some
research to try to reasonably follow what you are telling - despite
the fact that the immediate problem I had been trying to solve seems
to be solved.  By using deepcopy(), than overwriting the offending
attributes with fresh instantations.

But both Martin and JP bring copy_reg into the equation in solving
copy issues "by the book".

But the book at:

 3.18 copy -- Shallow and deep copy operations (from the 2.3 docs)

says,
on one hand:

"""Classes can use the same interfaces to control copying that they
use to control pickling"""

and on the other:

"""The copy module does not use the copy_reg registration module.""" 

This is actually pretty esoteric stuff for someone who has not delved
into these mysteries before, so I am a bit lost.

Is it that copy_reg comes into play in defining a custom method for
copying, that is called as a regular method, and not via the copy
module?

Does anyone have a reference for copy_reg used in the specific context
of copying, rather than pickling?

Art



More information about the Python-list mailing list