Search code examples
c++pybind11

How to specify a docstring for a c++ class in pybind11


Suppose I have a c++ class and want it to be available in Python using pybind11.
Is there notation for specifying a docstring for the class?
For example, this doesn't work:

  py::class_<Edge>(m, "Edge")
      .doc("An Edge is used for representing an edge of a graph.")
      .def(py::init<int>())

apparently because the doc method takes no arguments.


Solution

  • Try:

    PYBIND11_MODULE(example, m)
    {
          py::class_<Edge>(m, "Edge")
              .def(py::init<int>())
              .doc() = "An Edge is used for representing an edge of a graph.";
    }
    

    This is also valid:

    PYBIND11_MODULE(example, m)
    {
          py::class_<Edge>(m, "Edge");
    
          m.def(py::init<int>());
    
          m.doc() = "An Edge is used for representing an edge of a graph.";
    }