gpt4 book ai didi

python - pybind11 中的命名默认参数

转载 作者:行者123 更新时间:2023-12-02 11:38:22 34 4
gpt4 key购买 nike

我正在使用 pybind11 将 C++ 类方法包装在转换 lambda“shim”中(由于某些原因我必须这样做)。该方法的参数之一在 C++ 中是默认的。

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

在我的 pybind 代码中,我想保留这个可选参数:

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

如何在生成的 Python 代码中使 optMat 成为可选的命名参数?

最佳答案

只需将它们添加到 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());

关于python - pybind11 中的命名默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247515/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com