通过pybind11迭代Python包装的C ++对象

时间:2018-03-13 13:51:13

标签: python c++ pointers pybind11

我有C ++代码定义curves类,curve类和point类,我正在尝试通过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访问单个点,但我不知道如何迭代所有曲线,点等。

2 个答案:

答案 0 :(得分:1)

你可以def iterate_from(item): while item is not None: yield item item = item.next 一个简单的生成器:

for c in iterate_from(curves_instance.first):
    for p in iterate_from(c.first):
        print p.x1, ",", p.x2

这将允许您编写像bellow这样的代码(它只是一个概念,如果需要可以更新它)

// replace "Yes", "No" --> with, true, false if needed
@Html.RadioButtonFor(m => m.Compatible, "Yes", new { id = "compatible" })
@Html.Label("compatible", "Compatible")

@Html.RadioButtonFor(m => m.Compatible, "No", new { id = "notcompatible" })
@Html.Label("notcompatible", "Not Compatible")

答案 1 :(得分:0)

我认为正确的答案应该是 通过使用__iter__实现pybind11::make_iteratorhere提供了一个示例。

我认为是py::make_iterator

.def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
                         py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)