[C++-sig] Pointer to existing Python object

David Abrahams dave at boost-consulting.com
Fri May 20 13:03:36 CEST 2005


Andreas Beyer <beyer at imb-jena.de> writes:

> I think this is a very strong concept of general interest: I can now
> change Python attributs of wrappers (such as adding attributs in
> Python), although the objects were created and are managed by C++
> code.  (By management I mean live-time management.) There is no
> difficulty if you get an object from a C++ factory class and store
> it in a Python container. However, I want to store it in a C++
> container. In general, the two classes (the container and the
> factory) can of course be distinct. I don't think David's sollution
> is affected by this.

If you think there is something here of general interest, you might
want to rephrase that.  I _think_ I understand what you're saying, but
I'm not sure that others will.

FWIW, if you have total control over the C++ code involved, you can of course
convert objects to Python "greedily", or even create them in Python to
begin with:

      object py_a = class_<A, noncopyable>("A")
                         .def("self", &A::self)
                         .def_readwrite("val", &A::val)
                         ;
      
      // Now create A objects in Python via:

         object some_a = py_a();


Now you can store some_a in a C++ container.  Or you can


        shared_ptr<A> ap(extract<shared_ptr<A>(py_a));

and store ap, which will manage the lifetime of the same python
object.

HTH,

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




More information about the Cplusplus-sig mailing list