Skip to content

How-to nest multiple submodules #4027

Answered by jiwaszki
Sewtz asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Sewtz , I assume you want pure pybind solution. Here it is:

#include <pybind11/pybind11.h>

namespace py = pybind11;

size_t free_function() {
      return 42;
}

namespace module_a {
      size_t func() {
            return 97;
      }
}

namespace module_b {
      size_t func() {
            return 20;
      }
}

PYBIND11_MODULE(mymodule, m)
{
      m.doc() = "This is top module - mymodule.";
      m.def("free_function", &free_function);

      auto m_a = m.def_submodule("module_a", "This is A.");
      m_a.def("func", &module_a::func);

      auto m_b = m.def_submodule("module_b", "This is B.");
      m_b.def("func", &module_b::func);
}

You can either go with this pure pybind code …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@Sewtz
Comment options

Answer selected by Sewtz
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants