【发布时间】:2016-03-12 11:29:51
【问题描述】:
我有一个 C++ myObject 类,我使用包装器结构通过 boost python 公开:
struct myObjectWrapper{
static tuple compute(myObject& o,const Container& x0, const double& t0, Container& x){
double t;
int stat = o.evaluate(x0,t0,x,t);
return make_tuple(stat,t);
}
}
BOOST_PYTHON_MODULE(myModule)
{
// not shown here is code to expose Container class
class_<myObject>("MyObject")
.def("compute",&myObjectWrapper::compute)
;
}
容器目前定义为:
typedef std::valarray<double> Container
并且暴露于 python。
现在在 python 中我可以做到。
x = Container()
(status,t) = obj.compute(Container([0.,0.,0.]),0.0,x)
print status, t, x[0]
这不是很pythonic。我更愿意这样做:
(status,t,x) = obj.compute(Container([0.,0.,0.]),0.0)
print status, t, x[0]
我可以在 python 中编写一个额外的包装器,但我宁愿避免添加更多的包装器。
以下代码无法编译:
struct myObjectWrapper{
static tuple compute(myObject& o,const Container& x0, const double& t0){
double t;
Container x;
int stat = o.evaluate(x0,t0,x,t);
return make_tuple(stat,t,x);
}
}
另外我宁愿窃取局部变量 x 的内容并让 python 管理它而不是复制它:
return make_tuple(stat,t,std::move(x));
我如何做到这一点?
【问题讨论】:
标签: python c++ boost tuples boost-python