Advanced C++ / Python integration with Boost.Python (Part 3)


In this final part of the series I’m going to look at one further way we can use Boost.Python to help when using a C++ class within Python.

In the previous part we built a wrapper for a C++ class, exposing a number of class methods to Python.

However, in Python, it’s quite typical for a class to use properties as well as methods.

So to begin – let’s take a moment to look at properties within the context of classes in Python.

For example, if we had a Python class which looks like this:

We could then use it, something like this:

Whilst this works, we might reasonably say that the necessity to use getter and setter functions directly is not especially Pythonic – as it’s not how most built-in objects behave.

Instead we’d expect to be able to use it more like this:

We can do exactly this; if we use properties…

We just need to make a couple of changes to our Python code to enable this.

Note that substantively the code is unchanged; but we have renamed our two methods (giving them both the same name) and we’ve added two decorators – setting up the property (and thus implicitly the getter for that) and the explicit setup for the setter method.

Running this version in conjunction with the second usage example, we have a much more Pythonic implementation.

So the question is – can we do this when we’re wrapping a C++ class, with Boost.Python?

Well, of course the answer is yes!

Last time around we finished up with a Python class created using this call of the BOOST_PYTHON_MODULE macro.

We’ll leave the explicit way of setting the data with three discrete values (via set_data()) as-is, as that’s a slightly different use-case; but wouldn’t it be nice if we could use the import and export methods as the setter and getter of a bytes property?

It turns out that doing this is trivially easy. All we do is remove the .def lines for the importer and exporter methods; and replace them with one .add_property() line…

Now we can use the new bytes property from within Python by using:

That concludes my brief introduction on how to use Boost.Python to wrap your existing C++ code for use in Python. I hope you’ve found it useful or interesting.