You can use cython or Boost.Python to call native code from python. Since you are using C ++, I would recommend exploring Boost.Python , which offers a very natural way of wrapping C ++ classes for python.
As an example (next to what you provide), consider the following class definitions
class Bar { private: int value; public: Bar() : value(42){ } //Functions to expose to Python: int getValue() const { return value; } void setValue(int newValue) { value = newValue; } }; class Foo { private: //Integer Vector: std::vector<int> fooVector; Bar bar; public: //Functions to expose to Python: void pushBack(const int& newInt) { fooVector.push_back(newInt); } int getInt(const int& element) { return fooVector.at(element); } Bar& getBar() { return bar; } }; double compute() { return 18.3; }
This can be wrapped in python using Boost.Python
#include <boost/python.hpp> BOOST_PYTHON_MODULE(MyLibrary) { using namespace boost::python; class_<Foo>("Foo", init<>()) .def("pushBack", &Foo::pushBack, (arg("newInt"))) .def("getInt", &Foo::getInt, (arg("element"))) .def("getBar", &Foo::getBar, return_value_policy<reference_existing_object>()) ; class_<Bar>("Bar", init<>()) .def("getValue", &Bar::getValue) .def("setValue", &Bar::setValue, (arg("newValue"))) ; def("compute", compute); }
This code can be compiled into the static library MyLibrary.pyd and used like this
import MyLibrary foo = MyLibrary.Foo() foo.pushBack(10); foo.pushBack(20); foo.pushBack(30); print(foo.getInt(0))
source share