• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Boost.Python Tutorial</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="index.html" title="Boost.Python Tutorial">
8<link rel="next" href="tutorial/hello.html" title="Building Hello World">
9</head>
10<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
11<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
12<hr>
13<div class="spirit-nav"><a accesskey="n" href="tutorial/hello.html"><img src="../images/next.png" alt="Next"></a></div>
14<div class="article">
15<div class="titlepage">
16<div>
17<div><h2 class="title">
18<a name="tutorial"></a>Boost.Python Tutorial</h2></div>
19<div><div class="authorgroup">
20<div class="author"><h3 class="author">
21<span class="firstname">Joel</span> <span class="surname">de Guzman</span>
22</h3></div>
23<div class="author"><h3 class="author">
24<span class="firstname">David</span> <span class="surname">Abrahams</span>
25</h3></div>
26</div></div>
27<div><p class="copyright">Copyright © 2002-2005 Joel
28      de Guzman, David Abrahams</p></div>
29<div><div class="legalnotice">
30<a name="tutorial.legal"></a><p>
31        Distributed under the Boost Software License, Version 1.0. (See accompanying
32        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
33      </p>
34</div></div>
35</div>
36<hr>
37</div>
38<div class="toc">
39<p><b>Table of Contents</b></p>
40<dl class="toc">
41<dt><span class="section"><a href="index.html#tutorial.quickstart">QuickStart</a></span></dt>
42<dt><span class="section"><a href="tutorial/hello.html">Building Hello World</a></span></dt>
43<dt><span class="section"><a href="tutorial/exposing.html">Exposing Classes</a></span></dt>
44<dd><dl>
45<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.constructors">Constructors</a></span></dt>
46<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.class_data_members">Class Data Members</a></span></dt>
47<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.class_properties">Class Properties</a></span></dt>
48<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.inheritance">Inheritance</a></span></dt>
49<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.class_virtual_functions">Class Virtual
50      Functions</a></span></dt>
51<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.virtual_functions_with_default_i">Virtual
52      Functions with Default Implementations</a></span></dt>
53<dt><span class="section"><a href="tutorial/exposing.html#tutorial.exposing.class_operators_special_function">Class
54      Operators/Special Functions</a></span></dt>
55</dl></dd>
56<dt><span class="section"><a href="tutorial/functions.html">Functions</a></span></dt>
57<dd><dl>
58<dt><span class="section"><a href="tutorial/functions.html#tutorial.functions.call_policies">Call Policies</a></span></dt>
59<dt><span class="section"><a href="tutorial/functions.html#tutorial.functions.overloading">Overloading</a></span></dt>
60<dt><span class="section"><a href="tutorial/functions.html#tutorial.functions.default_arguments">Default Arguments</a></span></dt>
61<dt><span class="section"><a href="tutorial/functions.html#tutorial.functions.auto_overloading">Auto-Overloading</a></span></dt>
62</dl></dd>
63<dt><span class="section"><a href="tutorial/object.html">Object Interface</a></span></dt>
64<dd><dl>
65<dt><span class="section"><a href="tutorial/object.html#tutorial.object.basic_interface">Basic Interface</a></span></dt>
66<dt><span class="section"><a href="tutorial/object.html#tutorial.object.derived_object_types">Derived Object
67      types</a></span></dt>
68<dt><span class="section"><a href="tutorial/object.html#tutorial.object.extracting_c_objects">Extracting C++
69      objects</a></span></dt>
70<dt><span class="section"><a href="tutorial/object.html#tutorial.object.enums">Enums</a></span></dt>
71<dt><span class="section"><a href="tutorial/object.html#tutorial.object.creating_python_object">Creating <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span></code>
72      from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code></a></span></dt>
73</dl></dd>
74<dt><span class="section"><a href="tutorial/embedding.html">Embedding</a></span></dt>
75<dd><dl><dt><span class="section"><a href="tutorial/embedding.html#tutorial.embedding.using_the_interpreter">Using the interpreter</a></span></dt></dl></dd>
76<dt><span class="section"><a href="tutorial/iterators.html">Iterators</a></span></dt>
77<dt><span class="section"><a href="tutorial/exception.html">Exception Translation</a></span></dt>
78<dt><span class="section"><a href="tutorial/techniques.html">General Techniques</a></span></dt>
79<dd><dl>
80<dt><span class="section"><a href="tutorial/techniques.html#tutorial.techniques.creating_packages">Creating Packages</a></span></dt>
81<dt><span class="section"><a href="tutorial/techniques.html#tutorial.techniques.extending_wrapped_objects_in_pyt">Extending
82      Wrapped Objects in Python</a></span></dt>
83<dt><span class="section"><a href="tutorial/techniques.html#tutorial.techniques.reducing_compiling_time">Reducing
84      Compiling Time</a></span></dt>
85</dl></dd>
86</dl>
87</div>
88<div class="section">
89<div class="titlepage"><div><div><h2 class="title" style="clear: both">
90<a name="tutorial.quickstart"></a><a class="link" href="index.html#tutorial.quickstart" title="QuickStart">QuickStart</a>
91</h2></div></div></div>
92<p>
93      The Boost Python Library is a framework for interfacing Python and C++. It
94      allows you to quickly and seamlessly expose C++ classes functions and objects
95      to Python, and vice-versa, using no special tools -- just your C++ compiler.
96      It is designed to wrap C++ interfaces non-intrusively, so that you should not
97      have to change the C++ code at all in order to wrap it, making Boost.Python
98      ideal for exposing 3rd-party libraries to Python. The library's use of advanced
99      metaprogramming techniques simplifies its syntax for users, so that wrapping
100      code takes on the look of a kind of declarative interface definition language
101      (IDL).
102    </p>
103<h3>
104<a name="tutorial.quickstart.h0"></a>
105      <span class="phrase"><a name="tutorial.quickstart.hello_world"></a></span><a class="link" href="index.html#tutorial.quickstart.hello_world">Hello
106      World</a>
107    </h3>
108<p>
109      Following C/C++ tradition, let's start with the "hello, world". A
110      C++ Function:
111    </p>
112<pre class="programlisting"><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">greet</span><span class="special">()</span>
113<span class="special">{</span>
114   <span class="keyword">return</span> <span class="string">"hello, world"</span><span class="special">;</span>
115<span class="special">}</span>
116</pre>
117<p>
118      can be exposed to Python by writing a Boost.Python wrapper:
119    </p>
120<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
121
122<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello_ext</span><span class="special">)</span>
123<span class="special">{</span>
124    <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
125    <span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span> <span class="identifier">greet</span><span class="special">);</span>
126<span class="special">}</span>
127</pre>
128<p>
129      That's it. We're done. We can now build this as a shared library. The resulting
130      DLL is now visible to Python. Here's a sample Python session:
131    </p>
132<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello_ext</span>
133<span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">hello_ext</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
134<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
135</pre>
136<div class="blockquote"><blockquote class="blockquote"><p>
137        <span class="emphasis"><em><span class="bold"><strong>Next stop... Building your Hello World module
138        from start to finish...</strong></span></em></span>
139      </p></blockquote></div>
140</div>
141</div>
142<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
143<td align="left"><p><small>Last revised: August 11, 2020 at 14:59:42 GMT</small></p></td>
144<td align="right"><div class="copyright-footer"></div></td>
145</tr></table>
146<hr>
147<div class="spirit-nav"><a accesskey="n" href="tutorial/hello.html"><img src="../images/next.png" alt="Next"></a></div>
148</body>
149</html>
150