[C++-sig] shared_ptr object identity
Ralf W. Grosse-Kunstleve
rwgk at yahoo.com
Mon Aug 16 06:11:48 CEST 2010
> So I would wrap member functions returning the shared_ptr, and store a
> persistent reference to the python object, returning its shared_ptr
> > - 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
> None of these methods would allow me to extend the individual
> instances dynamically with attributes in python, correct?
Do I understand correctly that you want to use the object essentially like
a dictionary, just with obj.key instead of obj["key"]?
I consider this bad practice, since it isn't obvious to readers of your
Did you consider adding a boost::python::object member to your C++
object? That would solve all your problems I think and it would be
If you need to keep your C++ code independent of Python, you could
work with boost::any.
> 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?
I think you could make this work, but you'd have to maintain a registry.
I'd look for a more conventional solution first.
More information about the Cplusplus-sig