Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set just one default argument pybind11

I have a function :

void my_functions(int a, int b = 42);

And I want to bind it using only the default argument for b:

 m.def("my_functions", &my_functions, pb::arg("b") = 42); // just default for b

This doesn't work, I get:

/cache/venv/include/pybind11/pybind11.h:219:40: error: static assertion failed: The number of argument annotations does not match the number of function arguments
  219 |             expected_num_args<Extra...>(
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
  220 |                 sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),

What's the right way of doing it?

like image 413
Ivan Avatar asked Nov 15 '25 21:11

Ivan


1 Answers

This is how:

m.def("my_functions", &my_functions, pb::arg(), pb::arg("b") = 42)

See here: https://pybind11.readthedocs.io/en/stable/advanced/functions.html#non-converting-arguments

When specifying py::arg options it is necessary to provide the same number of options as the bound function has arguments. …

like image 163
HarryP2023 Avatar answered Nov 18 '25 12:11

HarryP2023



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!