>> I've been bashing my head against a wall for the past couple of days > >> trying to figure this out. It's frustrating because it seems like it >> should be such a simple thing. > > I cannot be so simple if you think of the C++ side as being unaware of > the Python layer. Each time the C++ function is called Boost.Python has > to create a new Python object. I believe it is very difficult or maybe > even impossible to reliably return the same Python object like you expect.
Well that's somewhat comforting to know :) >> Note that I am using a global mFooInstance for >> simplicity, but in reality such object will be held as members > > That still seems doable, via a wrapper object. > So I would wrap member functions returning the shared_ptr, and store a persistent reference to the python object, returning its shared_ptr instead? > - Add all data attributes to the C++ object (and use .def_readonly() or > .def_readwrite()). > Then the state of the object is preserved naturally. > > - Inject additional Python methods as described in the Boost.Python tutorial: > > http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/doc/html/python/techniques.html#python.extending_wrapped_objects_in_python None of these methods would allow me to extend the individual instances dynamically with attributes in python, correct? I suppose I could create some python code to wrap attributes and map to them from an ID on the object, but I can't imagine that would be terribly generic or concise. I stumbled upon this post in my search - http://mail.python.org/pipermail/cplusplus-sig/2006-March/010169.html. It talks about swapping out the shared_ptr with one that manages the Python object instead, so that presumably, subsequent usage of the shared_ptr will allow Python to know about the object. Unfortunately it doesn't seem to fit with my scenario, and is possibly more relevant to embedded python, but isn't there some way to write a custom to_python_converter to allow me to inject that code whenever a shared_ptr is to be converted to python? Earl _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig