What is the SWIG equivalent to storing a copy of an arbitrary python object?
I’m pretty sure what I’m asking is possible because it would work with boost::python (see below), but I can’t see a way to do this using SWIG.
#include <boost/python.hpp>
using namespace boost::python;
class MyClass
{
public:
// other operations
object get_info() { return info_; }
void set_info(object info) { info_ = info; }
private:
object info_;
};
BOOST_PYTHON_MODULE( mymodule )
{
class_<MyClass>("MyClass")
.def("get_info", &MyClass::get_info )
.def("set_info", &MyClass::set_info )
;
}
The simplest example is:
e.g.:
But note that in this case the ownership of the object has not been changed. If I’d done
foo.set_info("hi")instead then it would have been released because no references were retained by the time it calledfoo.get_info().You can fix that by adding a call to:
inside
set_info(), but then you’ll need a corresponding DECREF in the destructor, or ifset_info()is called when a reference is already held, or for copy construction or assignment. (Or some nice RAII type to do all that for you…)