您想要的是boost :: python :: scope。
Python没有“命名空间”的概念,但是您可以像命名空间一样使用类:
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/scope.hpp>
using namespace boost::python;
namespace a
{
class A{};
namespace b
{
class B{};
}
}
class DummyA{};
class DummyB{};
BOOST_PYTHON_MODULE(mymodule)
{
// Change the current scope
scope a
= class_<DummyA>("a")
;
// Define a class A in the current scope, a
class_<a::A>("A")
//.def("somemethod", &a::A::method)
;
// Change the scope again, a.b:
scope b
= class_<DummyB>("b")
;
class_<a::b::B>("B")
//.def("somemethod", &a::b::B::method)
;
}
然后在python中,您具有:
#!/usr/bin/env python
import mylib
print mylib.a,
print mylib.a.A
print mylib.a.b
print mylib.a.b.B
所有a
,a.A
,a.b
和a.b.B
实际上的类,但你可以把a
和a.b
就像命名空间-永不实际上的实例化