Search code examples
boost-python

Accessing variable in C++ that has been wrapped in Python


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.


Solution

  • 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);