Exercises
These are meant to be done as you go. If you get stuck, check the hints (or jump to Solutions).
Exercise 1 — Add documentation strings
Add a docstring to one exposed function using the third argument of m.def.
Success criteria:
help(yourmodule.yourfunction)shows your description.
Hint:
m.def("name", &fn, "This function ...")
Exercise 2 — Expose an overloaded function
Create two C++ functions with the same name but different signatures (e.g., add(int,int) and add(double,double))
and expose both in Python.
Success criteria:
- both variants are callable from Python without ambiguity.
Hint:
- pybind11 supports overload resolution; you may need
py::overload_cast<...>(...).
Exercise 3 — Use keyword arguments
Expose a function so that Python can call it with keyword arguments.
Success criteria:
fn(a=1, b=2)works.
Hint:
- use
py::arg("a"),py::arg("b")in the binding.