[section boost/python/call_method.hpp] [section Introduction] defines the call_method family of overloaded function templates, used to invoke callable attributes of Python objects from C++. [endsect] [section Function `call_method`] `` template R call_method(PyObject* self, char const* method, A1 const&, A2 const&, ... An const&) `` [variablelist [[Requires][`R` is a pointer type, reference type, or a complete type with an accessible copy constructor]] [[Effects][Invokes `self.method(a1, a2, ...an)` in Python, where `a1...an` are the arguments to `call_method()`, converted to Python objects. For a complete semantic description, see this page.]] [[Returns][The result of the Python call, converted to the C++ type `R`.]] [[Rationale][`call_method` is critical to implementing C++ virtual functions which are overridable in Python, as shown by the example below.]] ] [endsect] [section Example] The following C++ illustrates the use of `call_method` in wrapping a class with a virtual function that can be overridden in Python: C++ Module Definition `` #include #include #include #include // class to be wrapped class Base { public: virtual char const* class_name() const { return "Base"; } virtual ~Base(); }; bool is_base(Base* b) { return !std::strcmp(b->class_name(), "Base"); } // Wrapper code begins here using namespace boost::python; // Callback class class Base_callback : public Base { public: Base_callback(PyObject* self) : m_self(self) {} char const* class_name() const { return call_method(m_self, "class_name"); } char const* Base_name() const { return Base::class_name(); } private: PyObject* const m_self; }; using namespace boost::python; BOOST_PYTHON_MODULE(my_module) { def("is_base", is_base); class_("Base") .def("class_name", &Base_callback::Base_name) ; } `` Python code: `` >>> from my_module import * >>> class Derived(Base): ... def __init__(self): ... Base.__init__(self) ... def class_name(self): ... return self.__class__.__name__ ... >>> is_base(Base()) # calls the class_name() method from C++ 1 >>> is_base(Derived()) 0 `` [endsect] [endsect]