提振.Python多个返回参数

Boost.Python Multiple Return Arguments

本文关键字:返回 参数 Python 提振      更新时间:2023-10-16

我有一个c++函数,从它的参数中返回多个值。

void Do_Something( double input1, double input2, double input3,
    double& output1, double& output2 )
{
    ...
    output1 = something;
    output2 = something;
}

我想用Boost.Python包装这个函数。我已经提出了一个使用lambdas的解决方案,但它有点乏味,因为我有许多函数在其参数中有多个返回值。

BOOST_PYTHON_MODULE( mymodule )
{
    using boost::python;
    def( "Do_Something", +[]( double input1, double input2, double input3 )
    {
        double output1;
        double output2;
        Do_Something( input1, input2, input3, output1, output2 );
        return make_tuple( output1, output2 );
    });
}

在Boost.Python中是否有更好的自动方法来完成此任务?

可以这样改进:

boost::python::tuple Do_Something(double input1, double input2,
                                  double input3) {
    // Do something
    ...
    return boost::python::make_tuple(output1, output2);
}
BOOST_PYTHON_MODULE(mymodule) {
    def("Do_Something", Do_Something);
}