如何将我的对象类型的 Python 列表传递给ClassName
接受 a 的 C++ 函数vector<ClassName>
?
我发现的最好的是这样的:example。不幸的是,代码崩溃了,我似乎无法弄清楚原因。这是我使用的:
template<typename T>
void python_to_vector(boost::python::object o, vector<T>* v) {
try {
object iter_obj = object(handle<>(PyObject_GetIter(o.ptr())));
return;
for (;;) {
object obj = extract<object>(iter_obj.attr("next")());
// Should launch an exception if it cannot extract T
v->emplace_back(extract<T>(obj));
}
} catch(error_already_set) {
PyErr_Clear();
// If there is an exception (no iterator, extract failed or end of the
// list reached), clear it and exit the function
return;
}
}