boost python def 函數匯出

來源:互聯網
上載者:User

Introduction

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

這個函數可以協助我們將C++中的函數匯出到Python中,供Python使用。說實話,真的很好用。

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

  • 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.
  • 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
    .

    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.
  • 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.
Memnonic Name Requirements/Type properties Effects
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 thearity of fn. A copy will be used as the call policies of the resulting method overload.
Example例子,其實按照下面的例子,很容易就明白了。主要是
第一個參數,填寫你在Python中使用他的名字第二個參數,寫上函數指標第三個參數,填上參數名,好讓boost只要你的函數有幾個參數。第四個參數,是描述,可以忽略其實還有個很重要的是返回協議(return policy ),詳見: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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.