|
Boost.PythonHeader <boost/python/exec.hpp> |
eval
exec
exec_file
Exposes a mechanism for embedding the python interpreter into C++ code.
eval
object eval(str expression, object globals = object(), object locals = object());
expression
in the context
specified by the dictionaries globals
and locals
.
exec
object exec(str code, object globals = object(), object locals = object());
code
in the context
specified by the dictionaries globals
and locals
.
exec_file
object exec_file(str filename, object globals = object(), object locals = object());
filename
in the context specified by the dictionaries globals
and
locals
.
#include <iostream> #include <string> using namespace boost::python; void greet() { // Retrieve the main module. object main = import("__main__"); // Retrieve the main module's namespace object global(main.attr("__dict__")); // Define greet function in Python. object result = exec( "def greet(): \n" " return 'Hello from Python!' \n", global, global); // Create a reference to it. object greet = global["greet"]; // Call it. std::string message = extract<std::string>(greet()); std::cout << message << std::endl; }
def greet(): return 'Hello from Python!'
// ... // Load the greet function from a file. object result = exec_file(script, global, global); // ... }
Revised 01 November, 2005
© Copyright Stefan Seefeld 2005.