To configure a single default argument in pybind11, one can define the function using the py::arg
keyword and provide a default value for the argument. Here's an example:
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int x, int y=0) {
return x + y;
}
PYBIND11_MODULE(example, m) {
m.def("add", &add, py::arg("x"), py::arg("y")=0);
}
In this example, the add
function takes two arguments, x
and y
, with y
having a default value of 0
. To define this function in pybind11, we use the py::arg
keyword to specify the name of each argument and its default value.
Note that when using default arguments in pybind11, it's important to define the function in the correct order. Any default argument must come after all non-default arguments in the function definition.
Asked: 2022-06-01 11:00:00 +0000
Seen: 15 times
Last updated: Mar 01 '22