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:
class simple:
def __init__ (self):
self._data = None
def set_data(self, data):
self._data = data
def get_data(self):
return self._data
We could then use it, something like this:
import example
s = example.simple()
s.set_data(7)
print(s.get_data())
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:
import example
s=example.simple()
s.data = 7
print(s.data)
We can do exactly this; if we use properties…
Continue reading “Advanced C++ / Python integration with Boost.Python (Part 3)” →