1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Object Interface</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="up" href="../index.html" title="Boost.Python Tutorial"> 9<link rel="prev" href="functions.html" title="Functions"> 10<link rel="next" href="embedding.html" title="Embedding"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="functions.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="tutorial.object"></a><a class="link" href="object.html" title="Object Interface">Object Interface</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="object.html#tutorial.object.basic_interface">Basic Interface</a></span></dt> 24<dt><span class="section"><a href="object.html#tutorial.object.derived_object_types">Derived Object 25 types</a></span></dt> 26<dt><span class="section"><a href="object.html#tutorial.object.extracting_c_objects">Extracting C++ 27 objects</a></span></dt> 28<dt><span class="section"><a href="object.html#tutorial.object.enums">Enums</a></span></dt> 29<dt><span class="section"><a href="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> 30 from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code></a></span></dt> 31</dl></div> 32<p> 33 Python is dynamically typed, unlike C++ which is statically typed. Python variables 34 may hold an integer, a float, list, dict, tuple, str, long etc., among other 35 things. In the viewpoint of Boost.Python and C++, these Pythonic variables 36 are just instances of class <code class="literal">object</code>. We will see in this 37 chapter how to deal with Python objects. 38 </p> 39<p> 40 As mentioned, one of the goals of Boost.Python is to provide a bidirectional 41 mapping between C++ and Python while maintaining the Python feel. Boost.Python 42 C++ <code class="literal">object</code>s are as close as possible to Python. This should 43 minimize the learning curve significantly. 44 </p> 45<p> 46 <span class="inlinemediaobject"><img src="../../images/python.png"></span> 47 </p> 48<div class="section"> 49<div class="titlepage"><div><div><h3 class="title"> 50<a name="tutorial.object.basic_interface"></a><a class="link" href="object.html#tutorial.object.basic_interface" title="Basic Interface">Basic Interface</a> 51</h3></div></div></div> 52<p> 53 Class <code class="literal">object</code> wraps <code class="literal">PyObject*</code>. All the 54 intricacies of dealing with <code class="literal">PyObject</code>s such as managing 55 reference counting are handled by the <code class="literal">object</code> class. C++ 56 object interoperability is seamless. Boost.Python C++ <code class="literal">object</code>s 57 can in fact be explicitly constructed from any C++ object. 58 </p> 59<p> 60 To illustrate, this Python code snippet: 61 </p> 62<pre class="programlisting"><span class="keyword">def</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">):</span> 63 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">y</span> <span class="special">==</span> <span class="string">'foo'</span><span class="special">):</span> 64 <span class="identifier">x</span><span class="special">[</span><span class="number">3</span><span class="special">:</span><span class="number">7</span><span class="special">]</span> <span class="special">=</span> <span class="string">'bar'</span> 65 <span class="keyword">else</span><span class="special">:</span> 66 <span class="identifier">x</span><span class="special">.</span><span class="identifier">items</span> <span class="special">+=</span> <span class="identifier">y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> 67 <span class="keyword">return</span> <span class="identifier">x</span> 68 69<span class="keyword">def</span> <span class="identifier">getfunc</span><span class="special">():</span> 70 <span class="keyword">return</span> <span class="identifier">f</span><span class="special">;</span> 71</pre> 72<p> 73 Can be rewritten in C++ using Boost.Python facilities this way: 74 </p> 75<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">object</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> 76 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">y</span> <span class="special">==</span> <span class="string">"foo"</span><span class="special">)</span> 77 <span class="identifier">x</span><span class="special">.</span><span class="identifier">slice</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="number">7</span><span class="special">)</span> <span class="special">=</span> <span class="string">"bar"</span><span class="special">;</span> 78 <span class="keyword">else</span> 79 <span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"items"</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="identifier">x</span><span class="special">);</span> 80 <span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span> 81<span class="special">}</span> 82<span class="identifier">object</span> <span class="identifier">getfunc</span><span class="special">()</span> <span class="special">{</span> 83 <span class="keyword">return</span> <span class="identifier">object</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span> 84<span class="special">}</span> 85</pre> 86<p> 87 Apart from cosmetic differences due to the fact that we are writing the code 88 in C++, the look and feel should be immediately apparent to the Python coder. 89 </p> 90</div> 91<div class="section"> 92<div class="titlepage"><div><div><h3 class="title"> 93<a name="tutorial.object.derived_object_types"></a><a class="link" href="object.html#tutorial.object.derived_object_types" title="Derived Object types">Derived Object 94 types</a> 95</h3></div></div></div> 96<p> 97 Boost.Python comes with a set of derived <code class="literal">object</code> types 98 corresponding to that of Python's: 99 </p> 100<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 101<li class="listitem"> 102 list 103 </li> 104<li class="listitem"> 105 dict 106 </li> 107<li class="listitem"> 108 tuple 109 </li> 110<li class="listitem"> 111 str 112 </li> 113<li class="listitem"> 114 long_ 115 </li> 116<li class="listitem"> 117 enum 118 </li> 119</ul></div> 120<p> 121 These derived <code class="literal">object</code> types act like real Python types. 122 For instance: 123 </p> 124<pre class="programlisting"><span class="identifier">str</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==></span> <span class="string">"1"</span> 125</pre> 126<p> 127 Wherever appropriate, a particular derived <code class="literal">object</code> has 128 corresponding Python type's methods. For instance, <code class="literal">dict</code> 129 has a <code class="literal">keys()</code> method: 130 </p> 131<pre class="programlisting"><span class="identifier">d</span><span class="special">.</span><span class="identifier">keys</span><span class="special">()</span> 132</pre> 133<p> 134 <code class="literal">make_tuple</code> is provided for declaring <span class="emphasis"><em>tuple literals</em></span>. 135 Example: 136 </p> 137<pre class="programlisting"><span class="identifier">make_tuple</span><span class="special">(</span><span class="number">123</span><span class="special">,</span> <span class="char">'D'</span><span class="special">,</span> <span class="string">"Hello, World"</span><span class="special">,</span> <span class="number">0.0</span><span class="special">);</span> 138</pre> 139<p> 140 In C++, when Boost.Python <code class="literal">object</code>s are used as arguments 141 to functions, subtype matching is required. For example, when a function 142 <code class="literal">f</code>, as declared below, is wrapped, it will only accept 143 instances of Python's <code class="literal">str</code> type and subtypes. 144 </p> 145<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">name</span><span class="special">)</span> 146<span class="special">{</span> 147 <span class="identifier">object</span> <span class="identifier">n2</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"upper"</span><span class="special">)();</span> <span class="comment">// NAME = name.upper()</span> 148 <span class="identifier">str</span> <span class="identifier">NAME</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span> <span class="comment">// better</span> 149 <span class="identifier">object</span> <span class="identifier">msg</span> <span class="special">=</span> <span class="string">"%s is bigger than %s"</span> <span class="special">%</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span> 150<span class="special">}</span> 151</pre> 152<p> 153 In finer detail: 154 </p> 155<pre class="programlisting"><span class="identifier">str</span> <span class="identifier">NAME</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span> 156</pre> 157<p> 158 Illustrates that we provide versions of the str type's methods as C++ member 159 functions. 160 </p> 161<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">msg</span> <span class="special">=</span> <span class="string">"%s is bigger than %s"</span> <span class="special">%</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span> 162</pre> 163<p> 164 Demonstrates that you can write the C++ equivalent of <code class="literal">"format" 165 % x,y,z</code> in Python, which is useful since there's no easy way to 166 do that in std C++. 167 </p> 168<div class="blurb"> 169<div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div> 170<p> 171 <span class="inlinemediaobject"><img src="../../images/alert.png"></span> 172 <span class="bold"><strong>Beware</strong></span> the common pitfall of forgetting 173 that the constructors of most of Python's mutable types make copies, just 174 as in Python. 175 </p> 176</div> 177<p> 178 Python: 179 </p> 180<pre class="programlisting"><span class="special">>>></span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">dict</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">__dict__</span><span class="special">)</span> <span class="comment"># copies x.__dict__</span> 181<span class="special">>>></span> <span class="identifier">d</span><span class="special">[</span><span class="string">'whatever'</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span> <span class="comment"># modifies the copy</span> 182</pre> 183<p> 184 C++: 185 </p> 186<pre class="programlisting"><span class="identifier">dict</span> <span class="identifier">d</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span> <span class="comment">// copies x.__dict__</span> 187<span class="identifier">d</span><span class="special">[</span><span class="char">'whatever'</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="comment">// modifies the copy</span> 188</pre> 189<h3> 190<a name="tutorial.object.derived_object_types.h0"></a> 191 <span class="phrase"><a name="tutorial.object.derived_object_types.class_t_as_objects"></a></span><a class="link" href="object.html#tutorial.object.derived_object_types.class_t_as_objects">class_<T> 192 as objects</a> 193 </h3> 194<p> 195 Due to the dynamic nature of Boost.Python objects, any <code class="literal">class_<T></code> 196 may also be one of these types! The following code snippet wraps the class 197 (type) object. 198 </p> 199<p> 200 We can use this to create wrapped instances. Example: 201 </p> 202<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">vec345</span> <span class="special">=</span> <span class="special">(</span> 203 <span class="identifier">class_</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">>(</span><span class="string">"Vec2"</span><span class="special">,</span> <span class="identifier">init</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">>())</span> 204 <span class="special">.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"length"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">length</span><span class="special">)</span> 205 <span class="special">.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"angle"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">angle</span><span class="special">)</span> 206 <span class="special">)(</span><span class="number">3.0</span><span class="special">,</span> <span class="number">4.0</span><span class="special">);</span> 207 208<span class="identifier">assert</span><span class="special">(</span><span class="identifier">vec345</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">)</span> <span class="special">==</span> <span class="number">5.0</span><span class="special">);</span> 209</pre> 210</div> 211<div class="section"> 212<div class="titlepage"><div><div><h3 class="title"> 213<a name="tutorial.object.extracting_c_objects"></a><a class="link" href="object.html#tutorial.object.extracting_c_objects" title="Extracting C++ objects">Extracting C++ 214 objects</a> 215</h3></div></div></div> 216<p> 217 At some point, we will need to get C++ values out of object instances. This 218 can be achieved with the <code class="literal">extract<T></code> function. Consider 219 the following: 220 </p> 221<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">);</span> <span class="comment">// compile error</span> 222</pre> 223<p> 224 In the code above, we got a compiler error because Boost.Python <code class="literal">object</code> 225 can't be implicitly converted to <code class="literal">double</code>s. Instead, what 226 we wanted to do above can be achieved by writing: 227 </p> 228<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="identifier">o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">));</span> 229<span class="identifier">Vec2</span><span class="special">&</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&>(</span><span class="identifier">o</span><span class="special">);</span> 230<span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span> <span class="special">==</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">length</span><span class="special">());</span> 231</pre> 232<p> 233 The first line attempts to extract the "length" attribute of the 234 Boost.Python <code class="literal">object</code>. The second line attempts to <span class="emphasis"><em>extract</em></span> 235 the <code class="literal">Vec2</code> object from held by the Boost.Python <code class="literal">object</code>. 236 </p> 237<p> 238 Take note that we said "attempt to" above. What if the Boost.Python 239 <code class="literal">object</code> does not really hold a <code class="literal">Vec2</code> 240 type? This is certainly a possibility considering the dynamic nature of Python 241 <code class="literal">object</code>s. To be on the safe side, if the C++ type can't 242 be extracted, an appropriate exception is thrown. To avoid an exception, 243 we need to test for extractibility: 244 </p> 245<pre class="programlisting"><span class="identifier">extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&></span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span> 246<span class="keyword">if</span> <span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">check</span><span class="special">())</span> <span class="special">{</span> 247 <span class="identifier">Vec2</span><span class="special">&</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">();</span> <span class="special">...</span> 248</pre> 249<p> 250 <span class="inlinemediaobject"><img src="../../images/tip.png"></span> 251 The astute reader might have noticed that the <code class="literal">extract<T></code> 252 facility in fact solves the mutable copying problem: 253 </p> 254<pre class="programlisting"><span class="identifier">dict</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">dict</span><span class="special">>(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span> 255<span class="identifier">d</span><span class="special">[</span><span class="string">"whatever"</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="comment">// modifies x.__dict__ !</span> 256</pre> 257</div> 258<div class="section"> 259<div class="titlepage"><div><div><h3 class="title"> 260<a name="tutorial.object.enums"></a><a class="link" href="object.html#tutorial.object.enums" title="Enums">Enums</a> 261</h3></div></div></div> 262<p> 263 Boost.Python has a nifty facility to capture and wrap C++ enums. While Python 264 has no <code class="literal">enum</code> type, we'll often want to expose our C++ enums 265 to Python as an <code class="literal">int</code>. Boost.Python's enum facility makes 266 this easy while taking care of the proper conversions from Python's dynamic 267 typing to C++'s strong static typing (in C++, ints cannot be implicitly converted 268 to enums). To illustrate, given a C++ enum: 269 </p> 270<pre class="programlisting"><span class="keyword">enum</span> <span class="identifier">choice</span> <span class="special">{</span> <span class="identifier">red</span><span class="special">,</span> <span class="identifier">blue</span> <span class="special">};</span> 271</pre> 272<p> 273 the construct: 274 </p> 275<pre class="programlisting"><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">choice</span><span class="special">>(</span><span class="string">"choice"</span><span class="special">)</span> 276 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span> 277 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span> 278 <span class="special">;</span> 279</pre> 280<p> 281 can be used to expose to Python. The new enum type is created in the current 282 <code class="literal">scope()</code>, which is usually the current module. The snippet 283 above creates a Python class derived from Python's <code class="literal">int</code> 284 type which is associated with the C++ type passed as its first parameter. 285 </p> 286<div class="note"><table border="0" summary="Note"> 287<tr> 288<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td> 289<th align="left">Note</th> 290</tr> 291<tr><td align="left" valign="top"> 292<p> 293 <span class="bold"><strong>what is a scope?</strong></span> 294 </p> 295<p> 296 The scope is a class that has an associated global Python object which 297 controls the Python namespace in which new extension classes and wrapped 298 functions will be defined as attributes. Details can be found <a href="../../reference/high_level_components/boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.class_scope" target="_top">here</a>. 299 </p> 300</td></tr> 301</table></div> 302<p> 303 You can access those values in Python as 304 </p> 305<pre class="programlisting"><span class="special">>>></span> <span class="identifier">my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span> 306<span class="identifier">my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span> 307</pre> 308<p> 309 where my_module is the module where the enum is declared. You can also create 310 a new scope around a class: 311 </p> 312<pre class="programlisting"><span class="identifier">scope</span> <span class="identifier">in_X</span> <span class="special">=</span> <span class="identifier">class_</span><span class="special"><</span><span class="identifier">X</span><span class="special">>(</span><span class="string">"X"</span><span class="special">)</span> 313 <span class="special">.</span><span class="identifier">def</span><span class="special">(</span> <span class="special">...</span> <span class="special">)</span> 314 <span class="special">.</span><span class="identifier">def</span><span class="special">(</span> <span class="special">...</span> <span class="special">)</span> 315 <span class="special">;</span> 316 317<span class="comment">// Expose X::nested as X.nested</span> 318<span class="identifier">enum_</span><span class="special"><</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">nested</span><span class="special">>(</span><span class="string">"nested"</span><span class="special">)</span> 319 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span> 320 <span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span> 321 <span class="special">;</span> 322</pre> 323</div> 324<div class="section"> 325<div class="titlepage"><div><div><h3 class="title"> 326<a name="tutorial.object.creating_python_object"></a><a class="link" href="object.html#tutorial.object.creating_python_object" title="Creating boost::python::object from PyObject*">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> 327 from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code></a> 328</h3></div></div></div> 329<p> 330 When you want a <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> to manage a pointer to <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code> 331 pyobj one does: 332 </p> 333<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">o</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">handle</span><span class="special"><>(</span><span class="identifier">pyobj</span><span class="special">));</span> 334</pre> 335<p> 336 In this case, the <code class="computeroutput"><span class="identifier">o</span></code> object, 337 manages the <code class="computeroutput"><span class="identifier">pyobj</span></code>, it won’t 338 increase the reference count on construction. 339 </p> 340<p> 341 Otherwise, to use a borrowed reference: 342 </p> 343<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">o</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">handle</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">borrowed</span><span class="special">(</span><span class="identifier">pyobj</span><span class="special">)));</span> 344</pre> 345<p> 346 In this case, <code class="computeroutput"><span class="identifier">Py_INCREF</span></code> is 347 called, so <code class="computeroutput"><span class="identifier">pyobj</span></code> is not destructed 348 when object o goes out of scope. 349 </p> 350</div> 351</div> 352<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 353<td align="left"></td> 354<td align="right"><div class="copyright-footer">Copyright © 2002-2005 Joel 355 de Guzman, David Abrahams<p> 356 Distributed under the Boost Software License, Version 1.0. (See accompanying 357 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> 358 </p> 359</div></td> 360</tr></table> 361<hr> 362<div class="spirit-nav"> 363<a accesskey="p" href="functions.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../../images/next.png" alt="Next"></a> 364</div> 365</body> 366</html> 367