-
I am working on a library written in C++, which uses pybind11 to call python functions. I want to access and modify some objects in C++ (e.g.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You need to 1) import the module containing the bindings for #include <memory>
#include <iostream>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
PYBIND11_MAKE_OPAQUE(std::vector<double>);
PYBIND11_EMBEDDED_MODULE(module, m)
{
py::bind_vector<std::vector<double>>(m, "VectorDouble");
}
int main() {
std::vector<double> V{1, 2, 3};
py::scoped_interpreter guard{};
py::module_ cpp_module = py::module_::import("module");
py::module_ py_function = py::module_::import("py_function");
auto x = py_function.attr("double_all_values")(std::ref(V)); // or &V instead of std::ref(V)
for (const auto& v : V) {
std::cout << v << ' ';
}
std::cout << '\n';
return 0;
} |
Beta Was this translation helpful? Give feedback.
You need to 1) import the module containing the bindings for
std::vector<double>
and 2) wrap the value instd::ref
(or pass a pointer):