Method returning new instance of class?

"Martin v. Löwis" martin at v.loewis.de
Sat Sep 4 12:02:56 EDT 2004


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.

Regards,
Martin



More information about the Python-list mailing list