I have a C++ class that is exposed to python with boost python framework.
struct Var
{
Var(std::string name) : name(name), value() {}
std::string const name;
float value;
};
BOOST_PYTHON_MODULE(hello)
{
class_<Var>("Var", init<std::string>())
.def_readonly("name", &Var::name)
.def_readwrite("value", &Var::value);
;
}
Below is the python script using this class:
x = hello.Var("hello")
def print_func():
print(x.value)
Is it possible to access the object x inside C++ code and assign the value member variable a value in c++ that is printed when print_func() is executed in the python script ?
Varcan change its value as normal. e.g.void setValue(Var& v, float value) { v.value = value; }. You can then expose that function to python and call it to setx.value. Of course, since you definedvalueas areadwritemember, you can also just manipulatex.valuedirectly in python.xas python object usingattr()function, assign a value tovaluemember variable and then somehow push the updated object back to the python script ?void setValue(boost::python::object& obj, float value) { obj.attr("value") = value; }and then exposesetValueto python. Though I'm not sure why you would want to do this.