I'm using Boost Python to make C++ and Python operate together and I have a code that looks like this, creating an instance of a python object and calling one of its member functions.
bp::object myInstance;
// ... myInstance is initialized
bp::object fun = myInstance.attr("myfunction");
fun();
I would like to check if the member function exists before calling it. If it does not exist, I don't want to call.
The problem is: the call to myInstance.attr("myfunction") is successful even if the function does not exist. Hence, the only way to test if the function exists in my current code is by trying to call it, and catching an exception.
Is there any way to check if the function exists without involving exception or calling the function?
// ... myInstance is initialized
initializing a Python object in C++ is the one of the most poorly documented parts ofboost::python
and here you left it out! – caps Feb 10 '17 at 1:44