<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Chapter 1. python 2.0</title> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="index.html" title="Chapter 1. python 2.0"> <link rel="next" href="python/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="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a></div> <div class="chapter"> <div class="titlepage"><div> <div><h2 class="title"> <a name="python"></a>Chapter 1. python 2.0</h2></div> <div><div class="author"><h3 class="author"> <span class="firstname">Joel</span> <span class="surname">de Guzman</span> </h3></div></div> <div><div class="author"><h3 class="author"> <span class="firstname">David</span> <span class="surname">Abrahams</span> </h3></div></div> <div><p class="copyright">Copyright © 2002-2005 Joel de Guzman, David Abrahams</p></div> <div><div class="legalnotice"> <a name="id759709"></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></div> <div class="toc"> <p><b>Table of Contents</b></p> <dl> <dt><span class="section"><a href="index.html#python.quickstart">QuickStart</a></span></dt> <dt><span class="section"><a href="python/hello.html"> Building Hello World</a></span></dt> <dt><span class="section"><a href="python/exposing.html"> Exposing Classes</a></span></dt> <dd><dl> <dt><span class="section"><a href="python/exposing.html#python.constructors">Constructors</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.class_data_members">Class Data Members</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.class_properties">Class Properties</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.inheritance">Inheritance</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.class_virtual_functions">Class Virtual Functions</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.virtual_functions_with_default_implementations">Virtual Functions with Default Implementations</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.class_operators_special_functions">Class Operators/Special Functions</a></span></dt> </dl></dd> <dt><span class="section"><a href="python/functions.html">Functions</a></span></dt> <dd><dl> <dt><span class="section"><a href="python/functions.html#python.call_policies">Call Policies</a></span></dt> <dt><span class="section"><a href="python/functions.html#python.overloading">Overloading</a></span></dt> <dt><span class="section"><a href="python/functions.html#python.default_arguments">Default Arguments</a></span></dt> <dt><span class="section"><a href="python/functions.html#python.auto_overloading">Auto-Overloading</a></span></dt> </dl></dd> <dt><span class="section"><a href="python/object.html"> Object Interface</a></span></dt> <dd><dl> <dt><span class="section"><a href="python/object.html#python.basic_interface">Basic Interface</a></span></dt> <dt><span class="section"><a href="python/object.html#python.derived_object_types">Derived Object types</a></span></dt> <dt><span class="section"><a href="python/object.html#python.extracting_c___objects">Extracting C++ objects</a></span></dt> <dt><span class="section"><a href="python/object.html#python.enums">Enums</a></span></dt> </dl></dd> <dt><span class="section"><a href="python/embedding.html">Embedding</a></span></dt> <dd><dl><dt><span class="section"><a href="python/embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></dd> <dt><span class="section"><a href="python/iterators.html">Iterators</a></span></dt> <dt><span class="section"><a href="python/exception.html"> Exception Translation</a></span></dt> <dt><span class="section"><a href="python/techniques.html"> General Techniques</a></span></dt> <dd><dl> <dt><span class="section"><a href="python/techniques.html#python.creating_packages">Creating Packages</a></span></dt> <dt><span class="section"><a href="python/techniques.html#python.extending_wrapped_objects_in_python">Extending Wrapped Objects in Python</a></span></dt> <dt><span class="section"><a href="python/techniques.html#python.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="python.quickstart"></a>QuickStart</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> <a name="quickstart.hello_world"></a><h3> <a name="id759740"></a> Hello World </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"><</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">></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> <p> </p> <pre class="programlisting"><span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">hello_ext</span> <span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">hello</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> <p> </p> <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: July 01, 2010 at 21:56:58 GMT</small></p></td> <td align="right"><div class="copyright-footer"></div></td> </tr></table> <hr> <div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a></div> </body> </html>