|
|
Boost.PythonHeader <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&);
name is an ntbs which conforms to Python's identifier
naming rules.nameʇ趼a href=
"definitions.html#ntbs">ntbs㬲ⷻꏐython儼a href=
"http://www.python.org/doc/current/ref/identifiers.html">添uu饌⼯a>/dt>
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>ᣍ
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">䳔ꊽ Ϡͬᣍ
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ᣍ
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
ֺ쇃뼯p> |
Requirements/Type properties
Ҫdz/ Ѝʴ |
Effects
黼/p> |
|---|---|---|
| docstring | Any ntbs. | Value will be bound to the __doc__ attribute of
the resulting method overload.
ֵ밳樓ڽṻ儷֘Ԙ儼code>__doc__ʴᣍ |
| 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ږ芾⎊⎊ʽӚ |
A copy will be used as the call policies of the resulting
method overload.
һ躉᱾×罡黵ķ֘Ԙ儵瓃⟂ԡ㼯p> |
#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.