问题
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