How do you pass kwargs to a boost-python wrapped function?

后端 未结 1 1982
走了就别回头了
走了就别回头了 2020-12-09 04:36

I have a python function with this signature:

def post_message(self, message, *args, **kwargs):

I would like to call the function from c++

相关标签:
1条回答
  • 2020-12-09 04:56

    After some investigation, it turns out that the object function call operator is overridden for two arguments of type args_proxy and kwds_proxy. So you have to use this specific call style of two arguments.

    args_proxy and kwds_proxy are generated by the * overloads. This is really nice.

    Additionally, the first argument must be a tuple type so that the python interpreter correctly handles the *args argument.

    The resulting example works:

    boost::python::list arguments;
    arguments.append("aMessage");
    arguments.append("1");
    
    boost::python::dict options;
    options["source"] = "cpp";
    
    boost::python::object python_func = get_python_func_of_wrapped_object()
    python_func(*boost::python::tuple(arguments), **options)
    

    Hope this helps...

    0 讨论(0)
提交回复
热议问题