0

How do I access a variable in C++ that has been wrapped in Python via BoostPython method like below(in this case I want to access y):

boost::python::exec("y = x", main_namespace);

Thanks in advance.

EDIT: Assume y is an integer.

alfa_80
  • 427
  • 1
  • 5
  • 21

1 Answers1

1

All Python classes, functions, variables, etc. are contained in dicts. Since you seem to already have the main_namespace dict, you can just do this:

using namespace boost::python;

// .................................................

object y = main_namespace["y"];
std::string yString = extract<char const*>(y);
Aleksey Vitebskiy
  • 2,087
  • 1
  • 15
  • 14
  • Could you suggest if I want to use boost::python::ptr to accomplish the task? – alfa_80 Aug 01 '12 at 12:30
  • No, not for something this simple. boost::python::ptr is a reference to a boost::python::object. If you just want to get a value of a variable, just go straight for the object. – Aleksey Vitebskiy Jan 19 '13 at 03:32