Inheritance in Python C++ extension

c++, python

Solution

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 a `PyObject*` for the Python subclass instance, and that has a `notify` method that translates the arguments to Python objects, calls the `notify` method of the `PyObject*` (using, e.g., `PyObject_CallMethod`), translates the result back to C++ types, and returns.

Problem

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.

Original source

Related problems