C++ Boost


Header <boost/python/def.hpp>




def() is the function which can be used to expose C++ functions and callable objects as Python functions in the current scope.

def()ꯊ퓃Ӛ Ԛ屇Ӳ㨼code>scope㩍 ++ꯊ瓃攏㵼㶎ꐹthonꯊ


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: name is an ntbs which conforms to Python's identifier naming rules.

Ҫdz㺼/b>nameʇ趼a href= "definitions.html#ntbs">ntbs㬲ⷻꏐython儼a href= "http://www.python.org/doc/current/ref/identifiers.html">᪊添uu饌⼯a>᣼/dt>
  • If Fn is [derived from] object, it will be added to the current scope as a single overload. To be useful, fn should be callable.

    ȧ黼code>Fnʇ[ʼnɺה]object㬍 ˼玪奒떘ԘỌ迤ᇰӲᣍ ΪKӐӃ㬼code>fnӦ胊Ǽa href= "http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6">瓃儼/a>ᣍ

  • If a1 is the result of an overload-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's maximum arity.

    ȧ黼code>a1ʇ֘Ԙ疆ɱʽ㨯verload-dispatch-expression㩼/em> 儽ṻ㬔⽶Ԋ嚶Ўʽ㬲⇒fnᘐ늇ꯊ햸ի㬻⊇㉔ắʽָի㬍 ⢇҆車 href= "definitions.html#arity">ԪʽӫA1儼a href= "overloads.html#overload-dispatch-expression">׮䳔ꊽ Ϡͬᣍ

    Effects: For each prefix P of Fn's sequence of argument types, beginning with the one whose length is A1's minimum arity, adds a name(...) function overload to the current scope. Each overload generated invokes a1's call-expression with P, using a copy of a1's call policies. If the longest valid prefix of A1 contains N types and a1 holds M keywords, an initial sequence of the keywords are used for all but the first N - M arguments of each overload.

    黣꼯b>捍code>Fn儲Ί퀠ЍP儃︶ǰ׺P㬍 慠쓚 Ƥ㤶Ȏ꼣ode>A1儼a href= "overloads.html#overload-dispatch-expression">׮䳔ꊽ㬍 Ԛ屇Ӳ̭쓍 name(...)ꯊ햘Ԙᣍ y趉곉監ؔأ승Ӄa1儼a href= "CallPolicies.html">巔o߂Լ/a>儸᱾㬍 ӃP巔ucode>a1儵瓃᭴ 㨣all-expression㩡㍊ ȧ黼code>A1儗Ӑǰ׺ӐN趀Ѝ㬍 ⢇Ҽcode>a1ӐM趹ؼ엖㬍 ԃ︶֘Ԙ儋銬⎊ 嫳큋ͷ⿵ļem>N - M趣썊 ʹӃ阼엖儒븶㵊컯Pᣍ

  • 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 of a1-a3 are supplied, they may be selected in any order from the table below.

    籔⣬fnᘐ늇燿յĺ벳ɔắʽָի㬍 ⢇Ҽa href="scope.html">屇Ӳһ趎爆fn儵咻ꯊ햘Ԙᣍ ȧ黃듐a1-a3㬍 ˼CԈΒⴎ䓏±푡Աᣍ

Memnonic Name


Requirements/Type properties

Ҫdz/ Ѝʴ



docstring Any ntbs. Value will be bound to the __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 of a keyword-expression specifying no more arguments than the arity of fn.

阼엖᭴㨫eyword-expression㩼/em> 儽ṻ㬓Oږ芾⎊⎊ʽ⻶Ӛfn儼a href= "definitions.html#arity">Ԫʽᣍ

A copy will be used as the call policies of the resulting method overload.



#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"; }

    def("foo", foo, args("x", "y"), "foo's docstring");

7 March, 2003

© Copyright Dave Abrahams 2002.