Advertisement
dan-masek

Boost python, class bindings in multiple statements

Mar 9th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <boost/python.hpp>
  2.  
  3. namespace bp = boost::python;
  4.  
  5. struct test
  6. {
  7.     void one() {}
  8.     void two() {}
  9.     void three() {}
  10. };
  11.  
  12. int main()
  13. {
  14.     Py_Initialize();
  15.  
  16.     bp::object main_module = bp::import("__main__");
  17.     bp::object main_namespace = main_module.attr("__dict__");
  18.  
  19.     {
  20.         bp::class_<test> test_binding = bp::class_<test>("test");
  21.         test_binding.def("one", &test::one);
  22.         test_binding.def("two", &test::two);
  23.         test_binding.def("three", &test::three);
  24.  
  25.         main_namespace["test"] = test_binding;
  26.     }
  27.  
  28.     try {
  29.         exec("print dir(test)\n", main_namespace);
  30.     } catch (bp::error_already_set &) {
  31.         PyErr_Print();
  32.     }
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement