Use boost::python::make_function and provide a signature, since by default it does not handle std::function .
For example, we want to wrap the return type:
std::function<std::string(int, int)> get_string_function(const std::string& name) { return [=](int x, int y) { return name + "(x=" + std::to_string(x) + ", y=" + std::to_string(y) + ")"; }; }
We could define a shell and def using it:
boost::python::object get_string_function_pywrapper(const std::string& name) { auto func = get_string_function(name); auto call_policies = boost::python::default_call_policies(); typedef boost::mpl::vector<std::string, int, int> func_sig; return boost::python::make_function(func, call_policies, func_sig()); } BOOST_PYTHON_MODULE(s) { boost::python::def("get_string_function", get_string_function_pywrapper); }
Now the Python side can use the result as needed:
>>> import s >>> s.get_string_function("Coord") <Boost.Python.function object at 0x1cca450> >>> _(1, 4) 'Coord(x=1, y=4)'
source share