2
我有使用包裝結構經由升壓蟒露出C++ myObject的類:升壓蟒返回的元組包含自定義類型
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的內容和有蟒蛇管理它,而不是複製:
return make_tuple(stat,t,std::move(x));
如何我能達到這個目標嗎?
謝謝。有用。 –