3
我想从boost :: python包函数返回collections.namedtuple
的列表,但我不确定如何从C++代码创建这些对象。对于其他一些类型,有一个方便的包装器(例如字典),这使得这很容易,但没有像namedtuple那样存在。做这个的最好方式是什么?对字典的名单使用boost :: python创建python collections.namedtple使用boost :: python
现有代码:
namespace py = boost::python;
struct cacheWrap {
...
py::list getSources() {
py::list result;
for (auto& src : srcCache) { // srcCache is a C++ vector
// {{{ --> Want to use a namedtuple here instead of dict
py::dict pysrc;
pysrc["url"] = src.url;
pysrc["label"] = src.label;
result.append(pysrc);
// }}}
}
return result;
}
...
};
BOOST_PYTHON_MODULE(sole) {
py::class_<cacheWrap,boost::noncopyable>("doc", py::init<std::string>())
.def("getSources", &cacheWrap::getSources)
;
}
一个'namedtuple'是tuple'的'一个子类,所以也许你可以开始用处理前者的代码并相应地修改它。 – martineau