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

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello_ext</span><span class="special">)</span>
<span class="special">{</span>
    <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>
    <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>
<span class="special">}</span>
</pre>
<p>
      That's it. We're done. We can now build this as a shared library. The resulting
      DLL is now visible to Python. Here's a sample Python session:
    </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello_ext</span>
<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>
<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
</pre>
<div class="blockquote"><blockquote class="blockquote"><p>
        <span class="emphasis"><em><span class="bold"><strong>Next stop... Building your Hello World module
        from start to finish...</strong></span></em></span>
      </p></blockquote></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: December 14, 2015 at 09:15:13 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav"><a accesskey="n" href="tutorial/hello.html"><img src="../images/next.png" alt="Next"></a></div>
</body>
</html>