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?
Default arguments are overwritten when the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.
In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).
They are – Default, Keyword, and Arbitrary Arguments. Keyword arguments allow us to employ any order, whereas default arguments assist us to deal with the absence of values.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With