This is taken from the official boost python documentation.
Lets say you have a C++ class that looks like this:
class Vec2 {
public:
Vec2(double, double);
double length();
double angle();
};
You can derive the Python interface for it like that:
object py_vec2 = class_<Vec2>("Vec2", init<double, double>())
.def_readonly("length", &Point::length)
.def_readonly("angle", &Point::angle)
)
The class definition is now stored in the variable py_vec2
. Now I can create an instance of said class with:
object vec_instance = py_vec2(3.0, 4.0);
This instance can now be injected to a Python interpreter. E.g set it into a variable of the "__main__"
module:
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
main_namespace["vec_instance"] = vec_instance;