如何将 kwargs 传递给 boost-python 包装函数?
我有一个带有此签名的 python 函数:
def post_message(self, message, *args, **kwargs):
我想从 c++ 调用该函数并向其传递一些 kwargs。调用该函数不是问题。知道如何通过 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 中执行此代码时,我得到(这不是我想要的):
messsage = aMessage
args = (['1'], {'source': 'cpp'})
kwargs = {}
How do you pass the options in my example in the **kwargs字典?
我看过一篇 帖子 建议使用 **options 语法(这太酷了!):
python_func(message, arguments, **options)
不幸的是,这会导致
TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy
感谢您提供的任何帮助。
I have a python function with this signature:
def post_message(self, message, *args, **kwargs):
I would like to call the function from c++ and pass to it some kwargs. Calling the function is not the problem. Knowing how to pass the kwargs is. Here is a non-working paraphrased sample:
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)
When I exercise this code, in pdb I get (which is not what I would like):
messsage = aMessage
args = (['1'], {'source': 'cpp'})
kwargs = {}
How do you pass the options in my example in the **kwargs dictionary ?
I have seen one post suggesting to use the **options syntax (how cool is this!):
python_func(message, arguments, **options)
Unfortunately, this results in
TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy
Thank you for any help you can give.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番调查,发现对象函数调用运算符被
args_proxy
和kwds_proxy
类型的两个参数覆盖。 >。因此,您必须使用两个参数的特定调用方式。args_proxy
和kwds_proxy
由 * 重载生成。这真的很好。此外,第一个参数必须是元组类型,以便 python 解释器正确处理 *args 参数。
结果示例有效:
希望这有帮助......
After some investigation, it turns out that the object function call operator is overridden for two arguments of type
args_proxy
andkwds_proxy
. So you have to use this specific call style of two arguments.args_proxy
andkwds_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:
Hope this helps...