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 9270993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:30:27+00:00 2026-06-18T15:30:27+00:00

How do I access the boost::python::class_ object that’s been registered for a given C++

  • 0

How do I access the boost::python::class_ object that’s been registered for a given C++ class? I’m importing a boost::python module which defines a wrapper for boost::property_tree::ptree, but I would like to add additional methods to this wrapper definition. When I attempt to create a new wrapper, Boost Python complains that a handler has already been declared, and ignores my new definition.

Any ideas?

  • 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-06-18T15:30:28+00:00Added an answer on June 18, 2026 at 3:30 pm

    Following the suggestion of daramarak, as well as the Boost Python tutorial Extending Wrapped Objects In Python, I extended the class from within python. Python, and thus Boost::Python make little distinction between bound member functions and functions whose first argument is an object reference (or pointer). Thus you can define a function in C++ like so:

    bool ptree__contains(boost::property_tree::ptree* self, const std::string& key) {
        return self->find(key)!=self->not_found();
    }
    

    And then augment the imported class in Python like so:

    from other_module import ptree
    from my_module import ptree__contains
    
    # The __contains__ method is a special api function 
    # that enables "foo in bar" boolean test statements
    ptree.__contains__ = ptree__contains
    
    test_ptree = ptree()
    test_ptree.put("follow.the.yellow.brick.road", "OZ!")
    
    print "follow.the.yellow.brick.road" in test_ptree
    # > true
    

    I added my augmentation code to the __init__.py of my module, such that any imports of my module would automatically add the desired methods to the external object. I defined a function which modified the class, called this function, and then deleted it to clean up my namespace. Alternatively, you can exclude this function from your __all__ listing to keep it from being exported by from module import * statements. Works like a charm! Thanks again to daramarak.

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

Sidebar

Related Questions

How do I access a variable in C++ that has been wrapped in Python
I have a code that has been working for almost 4 years (since boost
I used boost::interprocess::managed_(windows_)shared_memory::construct to construct an interprocess vector holding an own class, which has
I have a Jamfile that I am using with Boost.Python to wrap some C++
Class member functions in Python have to explicitly declare a self parameter which represents
I have some code that I want to build. The code uses boost::ptr_map class
I have read that boost iostreams supposedly supports 64 bit access to large files
Is there, perhaps in boost, consistent element access semantics which works across containers? something
I have a class that is using boost::aligned storage to statically allocate memory within
I'm trying to write a class that allows data to be access by multiple

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.