###  | # Boost.Python ## Header <boost/python/make_function.hpp>

## Contents

Introduction
Functions
  make_function
  make_constructor
Example

## Introduction

`make_function()` and `make_constructor()` are the functions used internally by `def()` and `class_<>::def()` to produce Python callable objects which wrap C++ functions and member functions.

## Functions

    template <class F>
    object make_function(F f)

    template <class F, class Policies>
    object make_function(F f, Policies const& policies)

    template <class F, class Policies, class Keywords>
    object make_function(F f, Policies const& policies, Keywords const& keywords)

Requires: `F` is a function pointer or member function pointer type. If `policies` are supplied, it must be a model of CallPolicies. If `kewords` are supplied, it must be the result of a keyword-expression specifying no more arguments than the arity of `f`.
Effects: Creates a Python callable object which, when called from Python, converts its arguments to C++ and calls `f`. If `F` is a pointer-to-member-function type, the target object of the function call (`*this`) will be taken from the first Python argument, and subsequent Python arguments will be used as the arguments to `f`. If `policies` are supplied, it will be applied to the function as described here. If `keywords` are supplied, the keywords will be applied in order to the final arguments of the resulting function.
Returns: An instance of object which holds the new Python callable object.

    template <class T, class ArgList, class Generator>
    object make_constructor();

    template <class ArgList, class Generator, class Policies>
    object make_constructor(Policies const& policies)

Requires: `T` is a class type. `Policies` is a model of CallPolicies. `ArgList` is an MPL sequence of C++ argument types (A1, A2,... AN) such that if `a1, a2`... `aN` are objects of type A1, A2,... AN respectively, the expression `new Generator::apply<T>::type(a1, a2`... `aN`) is valid. Generator is a model of HolderGenerator.
Effects: Creates a Python callable object which, when called from Python, expects its first argument to be a Boost.Python extension class object. It converts its remaining its arguments to C++ and passes them to the constructor of a dynamically-allocated `Generator::apply<T>::type` object, which is then installed in the extension class object. In the second form, the `policies` are applied to the arguments and result (None) of the Python callable object
Returns: An instance of object which holds the new Python callable object.

## Example

C++ function exposed below returns a callable object wrapping one of two functions.

    #include <boost/python/make_function.hpp>
    #include <boost/python/module.hpp>

    char const* foo() { return "foo"; }
    char const* bar() { return "bar"; }

    using namespace boost::python;
    object choose_function(bool selector)
    {
        if (selector)
            return boost::python::make_function(foo);
        else
            return boost::python::make_function(bar);
    }

    BOOST_PYTHON_MODULE(make_function_test)
    {
        def("choose_function", choose_function);
    }

It can be used this way in Python:

    >>> from make_function_test import *
    >>> f = choose_function(1)
    >>> g = choose_function(0)
    >>> f()
    'foo'
    >>> g()
    'bar'

29 September 2002

© Copyright Dave Abrahams 2002. All Rights Reserved.