Introduction
def()Is the function which can be used to expose C ++ functions and callable objects as Python functions in the currentscope.
This function can be used to export functions in C ++ to Python. To be honest, it's really easy to use.
Functions
Def
template <class F>void def(char const* name, F f);template <class Fn, class A1>void def(char const* name, Fn fn, A1 const&);template <class Fn, class A1, class A2>void def(char const* name, Fn fn, A1 const&, A2 const&);template <class Fn, class A1, class A2, class A3>void def(char const* name, Fn fn, A1 const&, A2 const&, A3 const&);
Requires:nameIs an NTBs which conforms to Python's identifier
Naming rules.
-
- If
FnIs [derived from]object, It will be added to
Current scope as a single overload. To be useful,fnShocould be callable.
- If
a1Is the result ofOverload-Dispatch-expression, Only the second form
Is allowed and FN must be a pointer to function or pointer to member function whose Arity is the same as A1'sMaximum
Arity.
-
Effects:For each prefix POf
Fn'S sequence of argument types, beginning with the one whose length is A1'S Minimum
Arity, Adds name(... )Function overload to the current
Scope. Each overload generated invokes a1'S call-expression P, Using a copy a1'S call
Policies. If the longest valid prefix A1Contains NTypes and a1Holds MKeywords, an initial sequence of the keywords are used
For all but the first N- MArguments of each overload.
- Otherwise, FN must be a non-null function or member function pointer, and a single function overload built around FN is added to the current scope. If any
a1-a3Are
Supplied, they may be selected in any order from the table below.
| Memnonic name |
Requirements/Type Properties |
Effects |
| Docstring |
Any NTBs. |
Value will be bound to__doc__Attribute of the resulting method overload. |
| Policies |
A Model of callpolicies |
A copy will be used as the call policies of the resulting method overload. |
| Keywords |
The result ofKeyword-expressionSpecifying no more arguments than thearityfn. |
A copy will be used as the call policies of the resulting method overload. |
In the example below, it is easy to understand. Mainly
For the first parameter, enter the second parameter you used in Python, write the third parameter of the function pointer, and fill in the parameter name so that boost has several parameters as long as your function. The fourth parameter, is the description, can ignore in fact there is a very important return protocol (Return Policy), see: http://blog.csdn.net/cedricporter/article/details/6828322
#include <boost/python/def.hpp>#include <boost/python/module.hpp>#include <boost/python/args.hpp>using namespace boost::python;char const* foo(int x, int y) { return "foo"; }BOOST_PYTHON_MODULE(def_test){ def("foo", foo, args("x", "y"), "foo's docstring");}
7 March, 2003
Copyright Dave Abrahams 2002.