Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Boost python 如何将kwargs传递给boost python包装函数?_Boost Python_Keyword Argument - Fatal编程技术网

Boost python 如何将kwargs传递给boost python包装函数?

Boost python 如何将kwargs传递给boost python包装函数?,boost-python,keyword-argument,Boost Python,Keyword Argument,我有一个带有此签名的python函数: def post_message(self, message, *args, **kwargs): 我想从C++调用函数并传递给它一些KWARG。调用函数不是问题所在。知道如何通过kwargs是非常困难的。以下是一个非工作的释义示例: std::string message("aMessage"); boost::python::list arguments; arguments.append("1"); boost::python::dict opt

我有一个带有此签名的python函数:

def post_message(self, message, *args, **kwargs):
我想从C++调用函数并传递给它一些KWARG。调用函数不是问题所在。知道如何通过kwargs是非常困难的。以下是一个非工作的释义示例:

std::string message("aMessage");
boost::python::list arguments;
arguments.append("1");

boost::python::dict options;
options["source"] = "cpp";

boost::python::object python_func = get_python_func_of_wrapped_object()
python_func(message, arguments, options)
当我练习此代码时,在pdb中我得到(这不是我想要的):

如何在**kwargs字典中传递我的示例中的选项

我见过一个建议使用**选项语法的人(这有多酷!)

不幸的是,这导致了

TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy

感谢您提供的任何帮助。

经过调查,发现对象函数调用运算符对于类型为
args\u proxy
kwds\u proxy
的两个参数被覆盖。因此,您必须使用两个参数的特定调用样式

args\u proxy
kwds\u proxy
由*重载生成。这真是太好了

此外,第一个参数必须是元组类型,以便python解释器正确处理*args参数

由此产生的示例适用于:

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)

希望这有助于

哇,我不知道*和**重载,这是使boost::python令人钦佩的微小细节之一。请注意,即使只使用kwargs,也需要将tuple作为第一个参数。但它可以为空:
python\u func(*boost::python::tuple(),**选项)
TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy
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)