[C++-sig] Copying objects derived in python from C++?

Mark Williams mark at image-engine.com
Thu Jun 7 02:48:34 CEST 2007

Ralf W. Grosse-Kunstleve wrote:
> This could be connected to missing pickle support. I think copy.copy() 
> essentially pickles/unpickles to make the copy.
> I'd think if you add pickle support to your class it will work.
> I'm not sure about the details. See the "pickle suite" docs (link on 
> main Boost.Python page).
> Ralf

Thanks, this certainly seems to be the case. If I add 
".enable_pickling()" to my Base class definition, then the example I 
posted works as expected. However, if I then modify the Derived class 
constructor so that it takes an argument, e.g.:

class Derived(Base):
    def __init__(self, arg):

d = Derived(None)
d_copy = copy.copy(d)

I get an error such as:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    d_copy = copy.copy(d)
  File "python2.5/copy.py", line 95, in copy
    return _reconstruct(x, rv, 0)
  File "python2.5/copy.py", line 322, in _reconstruct
    y = callable(*args)
TypeError: __init__() takes exactly 2 arguments (1 given)


More information about the Cplusplus-sig mailing list