如何在 boost.python 中指定命名参数的值?

发布于 2024-11-19 02:03:51 字数 352 浏览 1 评论 0原文

我想将用 python 编写的函数嵌入到 c++ 代码中。
我的Python代码是:test.py

def func(x=None, y=None, z=None):  
  print x,y,z  

我的C++代码是:

module = import("test");  
namespace = module.attr("__dict__");  

//then i want to know how to pass value 'y' only.  
module.attr("func")("y=1") // is that right?

i want to embed a function written in python into c++ code.
My python code is:test.py

def func(x=None, y=None, z=None):  
  print x,y,z  

My c++ code is:

module = import("test");  
namespace = module.attr("__dict__");  

//then i want to know how to pass value 'y' only.  
module.attr("func")("y=1") // is that right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-11-26 02:03:52

我不确定 Boost.Python 是否实现了所声称的 ** 解引用运算符,但您仍然可以使用 Python C-API 来执行您正在测试的方法,如此处所述

以下是解决方案的原型:

//I'm starting from where you should change
boost::python::object callable = module.attr("func");

//Build your keyword argument dictionary using boost.python
boost::python::dict kw;
kw["x"] = 1;
kw["y"] = 3.14;
kw["z"] = "hello, world!";

//Note: This will return a **new** reference
PyObject* c_retval = PyObject_Call(callable.ptr(), NULL, kw.ptr());

//Converts a new (C) reference to a formal boost::python::object
boost::python::object retval(boost::python::handle<>(c_retval));

将返回值从 PyObject_Call 转换为正式的 boost::python::object 后,您可以从函数中返回它或者您可以忘记它,PyObject_Call 返回的新引用将被自动删除。

有关将 PyObject* 包装为 boost::python::object 的更多信息,请参阅 Boost.Python 教程。 更准确地说,在此链接,页面末尾

I'm not sure Boost.Python implements the ** dereference operator as claimed, but you can still use the Python C-API to execute the method you are intested on, as described here.

Here is a prototype of the solution:

//I'm starting from where you should change
boost::python::object callable = module.attr("func");

//Build your keyword argument dictionary using boost.python
boost::python::dict kw;
kw["x"] = 1;
kw["y"] = 3.14;
kw["z"] = "hello, world!";

//Note: This will return a **new** reference
PyObject* c_retval = PyObject_Call(callable.ptr(), NULL, kw.ptr());

//Converts a new (C) reference to a formal boost::python::object
boost::python::object retval(boost::python::handle<>(c_retval));

After you have converted the return value from PyObject_Call to a formal boost::python::object, you can either return it from your function or you can just forget it and the new reference returned by PyObject_Call will be auto-deleted.

For more information about wrapping PyObject* as boost::python::object, have a look at the Boost.Python tutorial. More precisely, at this link, end of the page.

鹿港小镇 2024-11-26 02:03:52

理论上的答案(没有时间自己尝试:-|):

boost::python::dict kw;
kw["y"]=1;
module.attr("func")(**kw); 

a theoretical answer (no time to try myself :-| ):

boost::python::dict kw;
kw["y"]=1;
module.attr("func")(**kw); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文