Named Default Arguments in pybind11

落花浮王杯 提交于 2020-01-23 19:45:18

问题


I'm using pybind11 to wrap a C++ class method in a conversion lambda "shim" (I must do this because reasons). One of the method's arguments is defaulted in C++.

class A
{
   void meow(Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity());
};

In my pybind code I want to preserve this optional parameter:

py::class_<A>(m, "A")
       .def(py::init<>())
       .def("meow",
            [](A& self, Eigen::Matrix4f optMat = Eigen::Matrix4f::Identity()) 
            {
               return self.meow( optMat ); 
            });

How do I make optMat an optional named argument in the generated Python code?


回答1:


Just add them after the lambda:

py::class_<A>(m, "A")
    .def(py::init<>())
    .def("meow",
         [](A& self, Eigen::Matrix4f optMat) {
             return self.meow(optMat); 
         },
         py::arg("optMat") = Eigen::Matrix4f::Identity());


来源:https://stackoverflow.com/questions/57247515/named-default-arguments-in-pybind11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!