#include using namespace boost::python; struct World { void set(std::string msg) { this->msg = msg; } std::string greet() { return msg; } void run() { handle<> main_module(borrowed( PyImport_AddModule("__main__") )); handle<> main_namespace(borrowed( PyModule_GetDict(main_module.get()) )); (handle<>( PyRun_String("hello = file('hello.txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, main_namespace.get(), main_namespace.get()) )); } private: std::string msg; }; BOOST_PYTHON_MODULE(hello) { class_("World") .def("greet", &World::greet) .def("set", &World::set) .def("run", &World::run) ; }