I have c++ library that need communicate with Python plugged in modules. Communication supposes implementing by Python some callback c++ interface.
I have read already about writing extensions, but no idea how to develop inheritance.
So something about:
C++:
class Broadcast
{
void set(Listener *){...
}
class Listener
{
void notify(Broadcast* owner) = 0;
}
I need something like in Python:
class ListenerImpl(Listener):
...
def notify(self, owner):
...
Note, I don’t want use Boost.
Writing Python types in C that are inheritable is explained in PEP 253. It’s not all that different from writing a normal builtin type as explained in the Extending/Embedding guide but you have to do certain things, like attribute access, through the Python API instead of accessing anything directly.
Exposing the Python subclasses back to C++ code is a little more tedious. The Python classes won’t be C++ subclasses, so you need a C++ wrapper class (that does inherit from
Listener) that contains aPyObject*for the Python subclass instance, and that has anotifymethod that translates the arguments to Python objects, calls thenotifymethod of thePyObject*(using, e.g.,PyObject_CallMethod), translates the result back to C++ types, and returns.