我有定义一个curves
类、一个curve
类和一个point
类的 C++ 代码,我正在尝试通过 pybind11 为这些类编写 Python 绑定并在 Python 中使用它们。
这些类的 pybind11 绑定如下所示:
namespace py = pybind11;
PYBIND11_MODULE(mymodule, m) {
py::class_<_point>(m, "_point")
.def(py::init<double, double>())
.def_readwrite("next", &point::next)
.def_readwrite("prev", &point::prev)
.def_readonly("x1", &point::x1)
.def_readonly("x2", &point::x2);
py::class_<curve>(m, "curve")
.def(py::init<point*>()) //constructor 1
.def(py::init()) //constructor 2
.def_readwrite("first", &curve::first)
.def_readwrite("last", &curve::last)
.def_readwrite("next", &curve::next)
.def_readwrite("prev", &curve::prev);
py::class_<curves>(m, "curves")
.def(py::init())
.def_readwrite("first", &curves::first)
.def_readwrite("last", &curves::last);
}
在 C++ 中,我可以迭代一个curves
对象,该对象由该对象组成,curve
而该对象又由point
以下对象组成:
for(curve *c=curves_pointer->first; c; c=c->next) {
for(point *p=c->first; p; p=p->next) {
cout << p->x1 << "," <<p->x2 << std::endl;
}
}
例如,在 Python 中,我可以使用 访问单个点curves_instance.last.first.x1
,但我不知道如何遍历所有曲线、点等。