Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.

C++ Boost

Boost.Python

Header <call_method.hpp>


Contents

Introduction
Functions
call_method
Example(s)

Introduction

<boost/python/call_method.hpp> defines the call_method family of overloaded function templates, used to invoke callable attributes of Python objects from C++.

Functions

template <class R, class A1, class A2, ... class An>
R call_method(PyObject* self, char const* method, A1 const&, A2 const&, ... An const&)
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.

Example(s)

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 <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/utility.hpp>
#include <cstring>

// 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<char const*>(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,Base_callback, noncopyable>("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

Revised 13 November, 2002

© Copyright Dave Abrahams 2002. All Rights Reserved.