Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:29:13+00:00 2026-05-26T22:29:13+00:00

Using boost python I need create nested namespace. Assume I have following cpp class

  • 0

Using boost python I need create nested namespace.

Assume I have following cpp class structure:

namespace a
{
    class A{...}
    namespace b
    {
         class B{...}
    }
}

Obvious solution not work:

BOOST_PYTHON_MODULE( a ) {
    boost::python::class_<a::A>("A")
     ...
    ;
    BOOST_PYTHON_MODULE(b){
        boost::python::class_<a::b::B>("B")
        ...
    ;
    }
}

It causes compile-time error: linkage specification must be at global scope

Is there any way to declare class B that would be accessed from Python as a.b.B?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:29:14+00:00Added an answer on May 26, 2026 at 10:29 pm

    What you want is a boost::python::scope.

    Python has no concept of ‘namespaces’, but you can use a class very much like a namespace:

    #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)
            ;
    }
    

    Then in python, you have:

    #!/usr/bin/env python
    import mylib
    
    print mylib.a,
    print mylib.a.A
    print mylib.a.b
    print mylib.a.b.B
    

    All a, a.A, a.b and a.b.B are actually classes, but you can treat a and a.b just like namespaces – and never actually instantiate them

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
I am new to boost python. I have to first init a cpp class
I'm using boost.python to make python-modules written in c++. I have some base class
I am trying to overload operators of a C++ class using Boost.Python. According to
I'm new in using boost and have a problem. I need shared_mutex function in
I am using boost shared pointers in my program, and I have a class
I'm trying to expose the following c++ function to python using boost.python: template <typename
I have problems importing a boost-python module on WinXP-32. I'm using python 2.6.6, boost.python
I'm exposing a C++ tree class using Boost.Python to python. The node class holds
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.