Discussion:
[C++-sig] C++ copy construction and Python object copies
Stefan Seefeld
2015-05-29 14:28:46 UTC
Permalink
Hello,

Python's copy module allows for objects to be copied. The protocol for
this will look up special method __copy__. It seems to me that this
would trivially work for C++ objects providing a copy-constructor.
However, the copy-constructor isn't automatically bound to __copy__.
While I can certainly add that in user-code, I wonder why this isn't
done by Boost.Python itself.
Does anyone know the reasons for this ? Would it seem useful to add that
feature ?

Stefan
--
...ich hab' noch einen Koffer in Berlin...
Alex Mohr
2015-05-29 22:48:00 UTC
Permalink
Post by Stefan Seefeld
Python's copy module allows for objects to be copied. The protocol for
this will look up special method __copy__. It seems to me that this
would trivially work for C++ objects providing a copy-constructor.
However, the copy-constructor isn't automatically bound to __copy__.
While I can certainly add that in user-code, I wonder why this isn't
done by Boost.Python itself.
Does anyone know the reasons for this ? Would it seem useful to add that
feature ?
The __copy__ method's intended semantics are to produce a "shallow" copy
while the __deepcopy__ method is meant to produce a "deep" copy. Given
a C++ class with a copy ctor, it's hard to know which is more
appropriate. For instance, copying a shared_ptr<T> is like a "shallow"
copy but copying a vector<pair<int, float>> is like a deep copy. On the
other hand copying a vector<shared_ptr<T>> is more like a shallow copy.

I wouldn't mind an opt-in convenience utility that adds a __copy__ or
__deepcopy__ method, but given the semantic subtlety, I think trying to
do it automatically is dicey.

Also, adding a feature to do it automatically could potentially
interfere with existing binding code that manually provides
__copy__/__deepcopy__ methods.

Alex
Stefan Seefeld
2015-05-29 22:57:29 UTC
Permalink
Post by Alex Mohr
Post by Stefan Seefeld
Python's copy module allows for objects to be copied. The protocol for
this will look up special method __copy__. It seems to me that this
would trivially work for C++ objects providing a copy-constructor.
However, the copy-constructor isn't automatically bound to __copy__.
While I can certainly add that in user-code, I wonder why this isn't
done by Boost.Python itself.
Does anyone know the reasons for this ? Would it seem useful to add that
feature ?
The __copy__ method's intended semantics are to produce a "shallow"
copy while the __deepcopy__ method is meant to produce a "deep" copy.
Given a C++ class with a copy ctor, it's hard to know which is more
appropriate. For instance, copying a shared_ptr<T> is like a
"shallow" copy but copying a vector<pair<int, float>> is like a deep
copy. On the other hand copying a vector<shared_ptr<T>> is more like
a shallow copy.
I wouldn't mind an opt-in convenience utility that adds a __copy__ or
__deepcopy__ method, but given the semantic subtlety, I think trying
to do it automatically is dicey.
That's a fair point.
Post by Alex Mohr
Also, adding a feature to do it automatically could potentially
interfere with existing binding code that manually provides
__copy__/__deepcopy__ methods.
True, but that could be mitigated by always letting an explicit addition
override the default.

Stefan
--
...ich hab' noch einen Koffer in Berlin...
Loading...