[英]Pybind11 - Function with unknown number of arguments
I want to get a list of arguments我想得到一个参数列表
my current c++ code:我当前的 C++ 代码:
m.def("test", [](std::vector<pybind11::object> args){
return ExecuteFunction("test", args);
});
my current python code:我当前的python代码:
module.test(["-2382.430176", "-610.183594", "12.673874"])
module.test([])
I want my python code to look like this:我希望我的 python 代码看起来像这样:
module.test("-2382.430176", "-610.183594", "12.673874")
module.test()
How can I get all arguments passed through Python?如何获取通过 Python 传递的所有参数?
Such generic functions module.test(*args)
can be created using pybind11:这样的通用函数module.test(*args)
可以使用 pybind11 创建:
void test(py::args args) {
// do something with arg
}
// Binding code
m.def("test", &test);
Or或者
m.def("test", [](py::args args){
// do something with arg
});
See Accepting *args and **kwargs and the example for more details.有关更多详细信息,请参阅接受 *args 和 **kwargs以及示例。
m.def("test", [](const pybind11::args& args){ std::vector<pybind11::object> pyArgs; for (const auto &arg : args) { pyArgs.push_back(arg.cast<pybind11::object>()); } return ExecuteFunction("test", pyArgs); });
I have to admit I'm not entirely sure what you're trying to do, but here is one idea:我不得不承认我不完全确定你想要做什么,但这里有一个想法:
def wrapper(args*)
return module.test(args)
There are other ways of making such a wrapper but this should work if I understand your question correctly.还有其他方法可以制作这样的包装器,但如果我正确理解您的问题,这应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.