-
I am working on a larger Framework in C++ which is divided into several namespaces like namespace Framework::ModA{
/* ModA stuff */
}
namespace Framework::ModB{
/* ModB stuff */
} Now I want to recreate such a structure in my python bindings. How can I achieve a structure like import Framework.ModA
import Framework.ModB |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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 and register submodules according to names of your namespaces (you can chain them as well) or register modules and re-arrange them with init files. So this is it, for example you can look up submodules in terminal using
|
Beta Was this translation helpful? Give feedback.
-
@jiwaszki I hope you don't mind that I'm asking a question about your example. There is something that has puzzled me for a bit. The line
defines a docstring for the submodule. But running |
Beta Was this translation helpful? Give feedback.
Hi @Sewtz , I assume you want pure pybind solution. Here it is:
You can either go with this pure pybind code …