@@ -365,6 +365,77 @@ Note that changes to the settings affect only function bindings created during t
365
365
lifetime of the ``options `` instance. When it goes out of scope at the end of the module's init function,
366
366
the default settings are restored to prevent unwanted side effects.
367
367
368
+ Overloaded functions
369
+ --------------------
370
+
371
+ The docstring of an overloaded function is prepended with the signature of each overload.
372
+ All overload docstrings are then concatenated together
373
+ into sections that are separated by each function signature.
374
+ The prepended signatures can be read by tools like Sphinx.
375
+
376
+ .. code-block :: cpp
377
+
378
+ PYBIND11_MODULE(example, m) {
379
+ m.def("add", [](int a, int b)->int { return a + b; },
380
+ "Add two integers together.");
381
+ m.def("add", [](float a, float b)->float { return a + b; },
382
+ "Add two floating point numbers together.");
383
+ }
384
+
385
+ The above example would produce the following docstring:
386
+
387
+ .. code-block :: pycon
388
+
389
+ >>> help(example.add)
390
+
391
+ add(...)
392
+ | add(arg0: int, arg1: int) -> int
393
+ | add(arg0: float, arg1: float) -> float
394
+ | Overloaded function.
395
+ |
396
+ | 1. add(arg0: int, arg1: int) -> int
397
+ |
398
+ | Add two integers together.
399
+ |
400
+ | 2. add(arg0: float, arg1: float) -> float
401
+ |
402
+ | Add two floating point numbers together.
403
+
404
+ Calling ``options.disable_function_signatures() `` as shown previously
405
+ will cause the docstrings of overloaded functions to be generated without the section headings.
406
+ The prepended overload signatures will remain:
407
+
408
+ .. code-block :: cpp
409
+
410
+ PYBIND11_MODULE(example, m) {
411
+ py::options options;
412
+ options.disable_function_signatures();
413
+
414
+ m.def("add", [](int a, int b)->int { return a + b; },
415
+ "A function which adds two numbers.\n"); // Note the additional newline here.
416
+ m.def("add", [](float a, float b)->float { return a + b; },
417
+ "Internally, a simple addition is performed.");
418
+ m.def("add", [](const py::none&, const py::none&)->py::none { return py::none(); },
419
+ "Both numbers can be None, and None will be returned.");
420
+ }
421
+
422
+ The above example would produce the following docstring:
423
+
424
+ .. code-block :: pycon
425
+
426
+ >>> help(example.add)
427
+ add(...)
428
+ | add(arg0: int, arg1: int) -> int
429
+ | add(arg0: float, arg1: float) -> float
430
+ | add(arg0: None, arg1: None) -> None
431
+ | A function which adds two numbers.
432
+ |
433
+ | Internally, a simple addition is performed.
434
+ | Both numbers can be None, and None will be returned.
435
+
436
+ Not every overload must supply a docstring.
437
+ You may find it easier for a single overload to supply the entire docstring.
438
+
368
439
.. [#f4 ] http://www.sphinx-doc.org
369
440
.. [#f5 ] http://github.com/pybind/python_example
370
441
0 commit comments