简体   繁体   中英

Named Default Arguments in pybind11

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?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM