i've question regarding boost-python. want execute specific python function stored in std::string boost-python. there example how achieve in documentation: boost python doc.
so i'm doing (c++ code):
using namespace boost::python; py_initialize(); // retrieve main module. object main = import("__main__"); // retrieve main module's namespace object global(main.attr("__dict__")); // define greet function in python. object result = exec(string_with_python_code.c_str(), global, global); object greet = global["greet"]; //calling greet() function greet(); py_finalize(); however, executes code not in function in global scope (contrary statement in documentation says above exec() statement: "[only] define greet function in python").
for example if set python code in string_with_python_code this:
string_with_python_code = "print 'hello global world!' \n" " \n" "def greet(): \n" " print 'hello local world!' \n" " return \n"; then sentence "hello global world!" printed out (before "hello local world!" printed out).
however, hoped accomplish function greet() gets executed. how can achieve this?
a python function definition execution of python code. python module import: importing module can cause arbitrary python statements run.
the way not run code outside function definition not pass code outside function definition exec. exec tell do, no less.
Comments
Post a Comment