[Python] pybind11: Python callback and C++ argument

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on Python bindings of my C++ library (a mathematical optimization solver) and I'm stuck at a point where I create a Python callback evaluate_constraints() that takes C++ arguments, pass it to the C++ library and evaluate it with C++ arguments. The callback modifies its second parameter constraints based on its first parameter x.

    // C++ code
    #include "Vector.hpp"
    #include <pybind11/pybind11.h>
    namespace py = pybind11;

    void solve(const std::function<void(const Vector&, Vector&)>& evaluate_constraints) {
    const Vector x = ...;
    Vector constraints = ...;
    evaluate_constraints(x, constraints);
    }

    PYBIND11_MODULE(unopy, module) {
    py::class_<Vector>(module, "Vector")
    .def(py::init<size_t>(), "Constructor")
    .def("__getitem__", [](const Vector& vector, size_t index) {
    return vector[index];
    })
    .def("__setitem__", [](Vector& vector, size_t index, double value) {
    vector[index] = value;
    });
    module.def("solve", &solve);
    }


    # Python code
    import myCppModule

    def evaluate_constraints(x, constraints):
    constraints[0] = function of x
    constraints[1] = function of x
    ...

    myCppModule.solve(evaluate_constraints)


    Unfortunately, some copy must happen somewhere, because the C++ object constraints is not modified. I'm not sure whether I missed something totally obvious (I've stumbled upon suggestions to use py::return_value_policy::reference_internal, but to no avail) or whether it is indeed a bit tricky to address. Hope you can crack it!

    Note: the second parameter is a Vector here, but for other callbacks, it could be a C++ matrix type.

    Continue reading...

Compartilhe esta Página