[C++-sig] Return value policy for property

David Abrahams dave at boost-consulting.com
Tue Dec 13 17:56:54 CET 2005


"Koen Van Herck" <koen_van_herck at yahoo.com> writes:

> --- Ralf wrote:
>> --- Koen Van Herck <koen_van_herck at yahoo.com> wrote:
>>
>> > I have a member function that I've wrapped as
>> > .def("getTime", &MyClass::getTime,
>> return_value_policy<manage_new_object>())
>> >
>> > Now I want to call the same function (getTime) as a Python property:
>> > .add_property("time", ???)
>> >
>> > How do I specify the 'manage_new_object' return value policy ?
>>
>> The trick is to use make_getter() and make_setter(). See the reference
>> documentation or grep for make_getter in boost/libs/python/test
>> to see some
>> examples (you should get hits in properties.cpp and data_members.cpp).
>>
> I think this does the opposite of what I want. make_getter allows me to
> write 'x.time()' in Python which returns 'Myclass::time' data member.
> What I want is to write 'x.time' in Python which calls 'const Time
> *MyClass::getTime()', and takes ownership of the pointer.

  .add_property(
       "time"
     , make_function( 
           &MyClass::getTime
         , return_value_policy<manage_new_object>()
       )
   )


HTH,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list