Skip to content

Instantly share code, notes, and snippets.

@Holt59
Created May 16, 2022 13:23
Show Gist options
  • Save Holt59/2f8d8dcbfed96491d3bc440a60d0016f to your computer and use it in GitHub Desktop.
Save Holt59/2f8d8dcbfed96491d3bc440a60d0016f to your computer and use it in GitHub Desktop.
Unpacking C++ type in Python with Pybind11
#include <pybind11/pybind11.h>
namespace py = pybind11;
struct Size {
double width, height;
};
PYBIND11_MODULE(demo, m) {
py::class_<Size>(m, "Size")
.def(py::init<double, double>())
.def_readwrite("width", &Size::width)
.def_readwrite("height", &Size::height)
.def("__iter__", [](Size const& v) {
return py::iter(py::make_tuple(v.width, v.height));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment