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?
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:
And then augment the imported class in Python like so:
I added my augmentation code to the
__init__.pyof 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 byfrom module import *statements. Works like a charm! Thanks again to daramarak.