@@ -302,6 +302,77 @@ Note that changes to the settings affect only function bindings created during t
302302lifetime of the ``options `` instance. When it goes out of scope at the end of the module's init function,
303303the default settings are restored to prevent unwanted side effects.
304304
305+ Overloaded functions
306+ --------------------
307+
308+ The docstring of an overloaded function is prepended with the signature of each overload.
309+ All overload docstrings are then concatenated together
310+ into sections that are separated by each function signature.
311+ The prepended signatures can be read by tools like Sphinx.
312+
313+ .. code-block :: cpp
314+
315+ PYBIND11_MODULE(example, m) {
316+ m.def("add", [](int a, int b)->int { return a + b; },
317+ "Add two integers together.");
318+ m.def("add", [](float a, float b)->float { return a + b; },
319+ "Add two floating point numbers together.");
320+ }
321+
322+ The above example would produce the following docstring:
323+
324+ .. code-block :: pycon
325+
326+ >>> help(example.add)
327+
328+ add(...)
329+ | add(arg0: int, arg1: int) -> int
330+ | add(arg0: float, arg1: float) -> float
331+ | Overloaded function.
332+ |
333+ | 1. add(arg0: int, arg1: int) -> int
334+ |
335+ | Add two integers together.
336+ |
337+ | 2. add(arg0: float, arg1: float) -> float
338+ |
339+ | Add two floating point numbers together.
340+
341+ Calling ``options.disable_function_signatures() `` as shown previously
342+ will cause the docstrings of overloaded functions to be generated without the section headings.
343+ The prepended overload signatures will remain:
344+
345+ .. code-block :: cpp
346+
347+ PYBIND11_MODULE(example, m) {
348+ py::options options;
349+ options.disable_function_signatures();
350+
351+ m.def("add", [](int a, int b)->int { return a + b; },
352+ "A function which adds two numbers.\n"); // Note the additional newline here.
353+ m.def("add", [](float a, float b)->float { return a + b; },
354+ "Internally, a simple addition is performed.");
355+ m.def("add", [](const py::none&, const py::none&)->py::none { return py::none(); },
356+ "Both numbers can be None, and None will be returned.");
357+ }
358+
359+ The above example would produce the following docstring:
360+
361+ .. code-block :: pycon
362+
363+ >>> help(example.add)
364+ add(...)
365+ | add(arg0: int, arg1: int) -> int
366+ | add(arg0: float, arg1: float) -> float
367+ | add(arg0: None, arg1: None) -> None
368+ | A function which adds two numbers.
369+ |
370+ | Internally, a simple addition is performed.
371+ | Both numbers can be None, and None will be returned.
372+
373+ Not every overload must supply a docstring.
374+ You may find it easier for a single overload to supply the entire docstring.
375+
305376.. [#f4 ] http://www.sphinx-doc.org
306377 .. [#f5 ] http://github.com/pybind/python_example
307378
0 commit comments