[C++-sig] return value policy for returning same python object...

Milind Patil milind_patil at hotmail.com
Sun Jun 8 19:59:15 CEST 2003


I have a scenario where a C++ method that performs inplace
operation on the object is wrapped using boost python. I
would like the corresponding python operation to result in the same object
instead of a new python object. What is the return policy that
will allow me to do that?

eg. in Y_Wrapper class of some Y class...

const Y&
do_iadd (int other)
{
  *this += Y_FromInt(other);
   return *this
}

BOOST_PYTHON_MODULE(hello)
{
  ...
.def("__iadd__", (const Y& Y_Wrapper::*) &do_iadd(int),
return_value_policy<>(??? which one ???))
...
}

in python...

x = y.hello(0)
a = x
x += 1

assert( a is x)

I tried return_existing_object and the copy_... policies. Obviuosly, because
they all return a new python
object, that failed. Any pointers as to how I can get the desired behaviour?
(Notice I cannot do
.def (self += other<int>())
because there is no += in the Y class for Y& and int defined.)

Thanks,
Milind










More information about the Cplusplus-sig mailing list