Consider a Callback class in Python.
class My_Callback():
def __init__(self):
pass
def do_something(self, something):
print(something)
This Class is used in a DLL wrapper which is using pybind11 to expose it to python. A reference to the Class is stored in an ENUM and passed to a C++ function.
void callPy(py::object f) {
//call f.do_something("test");
}
// ...
callPy(userData::myCallback);
There are many examples on how to call Python functions from C++ (pybind11 doc), but what is the intended way to call class methods? It feels like I should cast the py::object
to a My_Callback
, but I don’t see how to do it.
The following lines seems to do the job, but is this really the intended way to do it?
void callPy(py::object f) {
py::object mycallback = f.attr("do_something");
mycallback("test");
}