/usr/share/doc/pybind11-doc/html/classes.html is in pybind11-doc 2.0.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Object-oriented code — pybind11 2.0.1 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/theme_overrides.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="pybind11 2.0.1 documentation" href="index.html"/>
<link rel="next" title="Build systems" href="compiling.html"/>
<link rel="prev" title="First steps" href="basics.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> pybind11
</a>
<div class="version">
2.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="intro.html">About this project</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
</ul>
<p class="caption"><span class="caption-text">The Basics</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="basics.html">First steps</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Object-oriented code</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#creating-bindings-for-a-custom-type">Creating bindings for a custom type</a></li>
<li class="toctree-l2"><a class="reference internal" href="#keyword-and-default-arguments">Keyword and default arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#binding-lambda-functions">Binding lambda functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#instance-and-static-fields">Instance and static fields</a></li>
<li class="toctree-l2"><a class="reference internal" href="#dynamic-attributes">Dynamic attributes</a></li>
<li class="toctree-l2"><a class="reference internal" href="#inheritance">Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="#overloaded-methods">Overloaded methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="#enumerations-and-internal-types">Enumerations and internal types</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="compiling.html">Build systems</a></li>
</ul>
<p class="caption"><span class="caption-text">Advanced Topics</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="advanced/functions.html">Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/classes.html">Classes</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/exceptions.html">Exceptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/smart_ptrs.html">Smart pointers</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/cast/index.html">Type conversions</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/pycpp/index.html">Python C++ interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced/misc.html">Miscellaneous</a></li>
</ul>
<p class="caption"><span class="caption-text">Extra Information</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmark.html">Benchmark</a></li>
<li class="toctree-l1"><a class="reference internal" href="limitations.html">Limitations</a></li>
<li class="toctree-l1"><a class="reference internal" href="reference.html">Reference</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">pybind11</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Object-oriented code</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/classes.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="object-oriented-code">
<span id="classes"></span><h1>Object-oriented code<a class="headerlink" href="#object-oriented-code" title="Permalink to this headline">¶</a></h1>
<div class="section" id="creating-bindings-for-a-custom-type">
<h2>Creating bindings for a custom type<a class="headerlink" href="#creating-bindings-for-a-custom-type" title="Permalink to this headline">¶</a></h2>
<p>Let’s now look at a more complex example where we’ll create bindings for a
custom C++ data structure named <code class="docutils literal"><span class="pre">Pet</span></code>. Its definition is given below:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">Pet</span> <span class="p">{</span>
<span class="n">Pet</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">setName</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name_</span><span class="p">)</span> <span class="p">{</span> <span class="n">name</span> <span class="o">=</span> <span class="n">name_</span><span class="p">;</span> <span class="p">}</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">getName</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">name</span><span class="p">;</span> <span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The binding code for <code class="docutils literal"><span class="pre">Pet</span></code> looks as follows:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><pybind11/pybind11.h></span><span class="cp"></span>
<span class="k">namespace</span> <span class="n">py</span> <span class="o">=</span> <span class="n">pybind11</span><span class="p">;</span>
<span class="n">PYBIND11_PLUGIN</span><span class="p">(</span><span class="n">example</span><span class="p">)</span> <span class="p">{</span>
<span class="n">py</span><span class="o">::</span><span class="n">module</span> <span class="n">m</span><span class="p">(</span><span class="s">"example"</span><span class="p">,</span> <span class="s">"pybind11 example plugin"</span><span class="p">);</span>
<span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"setName"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">setName</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"getName"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">getName</span><span class="p">);</span>
<span class="k">return</span> <span class="n">m</span><span class="p">.</span><span class="n">ptr</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p><code class="xref cpp cpp-class docutils literal"><span class="pre">class_</span></code> creates bindings for a C++ <cite>class</cite> or <cite>struct</cite>-style data
structure. <code class="xref cpp cpp-func docutils literal"><span class="pre">init()</span></code> is a convenience function that takes the types of a
constructor’s parameters as template arguments and wraps the corresponding
constructor (see the <a class="reference internal" href="advanced/classes.html#custom-constructors"><span class="std std-ref">Custom constructors</span></a> section for details). An
interactive Python session demonstrating this example is shown below:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="go">% python</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">example</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">example</span><span class="o">.</span><span class="n">Pet</span><span class="p">(</span><span class="s1">'Molly'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go"><example.Pet object at 0x10cd98060></span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">getName</span><span class="p">()</span>
<span class="go">u'Molly'</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">setName</span><span class="p">(</span><span class="s1">'Charly'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">getName</span><span class="p">()</span>
<span class="go">u'Charly'</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">Static member functions can be bound in the same way using
<code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_static()</span></code>.</p>
</div>
</div>
<div class="section" id="keyword-and-default-arguments">
<h2>Keyword and default arguments<a class="headerlink" href="#keyword-and-default-arguments" title="Permalink to this headline">¶</a></h2>
<p>It is possible to specify keyword and default arguments using the syntax
discussed in the previous chapter. Refer to the sections <a class="reference internal" href="basics.html#keyword-args"><span class="std std-ref">Keyword arguments</span></a>
and <a class="reference internal" href="basics.html#default-args"><span class="std std-ref">Default arguments</span></a> for details.</p>
</div>
<div class="section" id="binding-lambda-functions">
<h2>Binding lambda functions<a class="headerlink" href="#binding-lambda-functions" title="Permalink to this headline">¶</a></h2>
<p>Note how <code class="docutils literal"><span class="pre">print(p)</span></code> produced a rather useless summary of our data structure in the example above:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go"><example.Pet object at 0x10cd98060></span>
</pre></div>
</div>
<p>To address this, we could bind an utility function that returns a human-readable
summary to the special method slot named <code class="docutils literal"><span class="pre">__repr__</span></code>. Unfortunately, there is no
suitable functionality in the <code class="docutils literal"><span class="pre">Pet</span></code> data structure, and it would be nice if
we did not have to change it. This can easily be accomplished by binding a
Lambda function instead:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"setName"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">setName</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"getName"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">getName</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"__repr__"</span><span class="p">,</span>
<span class="p">[](</span><span class="k">const</span> <span class="n">Pet</span> <span class="o">&</span><span class="n">a</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="s">"<example.Pet named '"</span> <span class="o">+</span> <span class="n">a</span><span class="p">.</span><span class="n">name</span> <span class="o">+</span> <span class="s">"'>"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">);</span>
</pre></div>
</div>
<p>Both stateless <a class="footnote-reference" href="#f1" id="id1">[1]</a> and stateful lambda closures are supported by pybind11.
With the above change, the same Python code now produces the following output:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="go"><example.Pet named 'Molly'></span>
</pre></div>
</div>
<table class="docutils footnote" frame="void" id="f1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Stateless closures are those with an empty pair of brackets <code class="docutils literal"><span class="pre">[]</span></code> as the capture object.</td></tr>
</tbody>
</table>
</div>
<div class="section" id="instance-and-static-fields">
<span id="properties"></span><h2>Instance and static fields<a class="headerlink" href="#instance-and-static-fields" title="Permalink to this headline">¶</a></h2>
<p>We can also directly expose the <code class="docutils literal"><span class="pre">name</span></code> field using the
<code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_readwrite()</span></code> method. A similar <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_readonly()</span></code>
method also exists for <code class="docutils literal"><span class="pre">const</span></code> fields.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">)</span>
<span class="c1">// ... remainder ...</span>
</pre></div>
</div>
<p>This makes it possible to write</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">example</span><span class="o">.</span><span class="n">Pet</span><span class="p">(</span><span class="s1">'Molly'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span>
<span class="go">u'Molly'</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'Charly'</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span>
<span class="go">u'Charly'</span>
</pre></div>
</div>
<p>Now suppose that <code class="docutils literal"><span class="pre">Pet::name</span></code> was a private internal variable
that can only be accessed via setters and getters.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Pet</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="n">Pet</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">setName</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name_</span><span class="p">)</span> <span class="p">{</span> <span class="n">name</span> <span class="o">=</span> <span class="n">name_</span><span class="p">;</span> <span class="p">}</span>
<span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">getName</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">name</span><span class="p">;</span> <span class="p">}</span>
<span class="k">private</span><span class="o">:</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>In this case, the method <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_property()</span></code>
(<code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_property_readonly()</span></code> for read-only data) can be used to
provide a field-like interface within Python that will transparently call
the setter and getter functions:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_property</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">getName</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">setName</span><span class="p">)</span>
<span class="c1">// ... remainder ...</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">Similar functions <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_readwrite_static()</span></code>,
<code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_readonly_static()</span></code> <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_property_static()</span></code>,
and <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_property_readonly_static()</span></code> are provided for binding
static variables and properties. Please also see the section on
<a class="reference internal" href="advanced/classes.html#static-properties"><span class="std std-ref">Static properties</span></a> in the advanced part of the documentation.</p>
</div>
</div>
<div class="section" id="dynamic-attributes">
<h2>Dynamic attributes<a class="headerlink" href="#dynamic-attributes" title="Permalink to this headline">¶</a></h2>
<p>Native Python classes can pick up new attributes dynamically:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">Pet</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">name</span> <span class="o">=</span> <span class="s1">'Molly'</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">Pet</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'Charly'</span> <span class="c1"># overwrite existing</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># dynamically add a new attribute</span>
</pre></div>
</div>
<p>By default, classes exported from C++ do not support this and the only writable
attributes are the ones explicitly defined using <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_readwrite()</span></code>
or <code class="xref cpp cpp-func docutils literal"><span class="pre">class_::def_property()</span></code>.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">);</span>
</pre></div>
</div>
<p>Trying to set any other attribute results in an error:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">example</span><span class="o">.</span><span class="n">Pet</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'Charly'</span> <span class="c1"># OK, attribute defined in C++</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># fail</span>
<span class="go">AttributeError: 'Pet' object has no attribute 'age'</span>
</pre></div>
</div>
<p>To enable dynamic attributes for C++ classes, the <code class="xref cpp cpp-class docutils literal"><span class="pre">py::dynamic_attr</span></code> tag
must be added to the <code class="xref cpp cpp-class docutils literal"><span class="pre">py::class_</span></code> constructor:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">dynamic_attr</span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">);</span>
</pre></div>
</div>
<p>Now everything works as expected:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">example</span><span class="o">.</span><span class="n">Pet</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">'Charly'</span> <span class="c1"># OK, overwrite value in C++</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># OK, dynamically add a new attribute</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="vm">__dict__</span> <span class="c1"># just like a native Python class</span>
<span class="go">{'age': 2}</span>
</pre></div>
</div>
<p>Note that there is a small runtime cost for a class with dynamic attributes.
Not only because of the addition of a <code class="docutils literal"><span class="pre">__dict__</span></code>, but also because of more
expensive garbage collection tracking which must be activated to resolve
possible circular references. Native Python classes incur this same cost by
default, so this is not anything to worry about. By default, pybind11 classes
are more efficient than native Python classes. Enabling dynamic attributes
just brings them on par.</p>
</div>
<div class="section" id="inheritance">
<span id="id2"></span><h2>Inheritance<a class="headerlink" href="#inheritance" title="Permalink to this headline">¶</a></h2>
<p>Suppose now that the example consists of two data structures with an
inheritance relationship:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">Pet</span> <span class="p">{</span>
<span class="n">Pet</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
<span class="p">};</span>
<span class="k">struct</span> <span class="nl">Dog</span> <span class="p">:</span> <span class="n">Pet</span> <span class="p">{</span>
<span class="n">Dog</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="n">Pet</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">bark</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="s">"woof!"</span><span class="p">;</span> <span class="p">}</span>
<span class="p">};</span>
</pre></div>
</div>
<p>There are two different ways of indicating a hierarchical relationship to
pybind11: the first specifies the C++ base class as an extra template
parameter of the <code class="xref cpp cpp-class docutils literal"><span class="pre">class_</span></code>:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">);</span>
<span class="c1">// Method 1: template parameter:</span>
<span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Dog</span><span class="p">,</span> <span class="n">Pet</span> <span class="cm">/* <- specify C++ parent type */</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Dog"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"bark"</span><span class="p">,</span> <span class="o">&</span><span class="n">Dog</span><span class="o">::</span><span class="n">bark</span><span class="p">);</span>
</pre></div>
</div>
<p>Alternatively, we can also assign a name to the previously bound <code class="docutils literal"><span class="pre">Pet</span></code>
<code class="xref cpp cpp-class docutils literal"><span class="pre">class_</span></code> object and reference it when binding the <code class="docutils literal"><span class="pre">Dog</span></code> class:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span> <span class="n">pet</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">);</span>
<span class="n">pet</span><span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">);</span>
<span class="c1">// Method 2: pass parent class_ object:</span>
<span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Dog</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Dog"</span><span class="p">,</span> <span class="n">pet</span> <span class="cm">/* <- specify Python parent type */</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"bark"</span><span class="p">,</span> <span class="o">&</span><span class="n">Dog</span><span class="o">::</span><span class="n">bark</span><span class="p">);</span>
</pre></div>
</div>
<p>Functionality-wise, both approaches are equivalent. Afterwards, instances will
expose fields and methods of both types:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">example</span><span class="o">.</span><span class="n">Dog</span><span class="p">(</span><span class="s1">'Molly'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">name</span>
<span class="go">u'Molly'</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">bark</span><span class="p">()</span>
<span class="go">u'woof!'</span>
</pre></div>
</div>
</div>
<div class="section" id="overloaded-methods">
<h2>Overloaded methods<a class="headerlink" href="#overloaded-methods" title="Permalink to this headline">¶</a></h2>
<p>Sometimes there are several overloaded C++ methods with the same name taking
different kinds of input arguments:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">Pet</span> <span class="p">{</span>
<span class="n">Pet</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">,</span> <span class="kt">int</span> <span class="n">age</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">age</span><span class="p">(</span><span class="n">age</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">set</span><span class="p">(</span><span class="kt">int</span> <span class="n">age</span><span class="p">)</span> <span class="p">{</span> <span class="n">age</span> <span class="o">=</span> <span class="n">age</span><span class="p">;</span> <span class="p">}</span>
<span class="kt">void</span> <span class="n">set</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span> <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="p">;</span> <span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">age</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>Attempting to bind <code class="docutils literal"><span class="pre">Pet::set</span></code> will cause an error since the compiler does not
know which method the user intended to select. We can disambiguate by casting
them to function pointers. Binding multiple functions to the same Python name
automatically creates a chain of function overloads that will be tried in
sequence.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="p">,</span> <span class="kt">int</span><span class="o">></span><span class="p">())</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"set"</span><span class="p">,</span> <span class="p">(</span><span class="kt">void</span> <span class="p">(</span><span class="n">Pet</span><span class="o">::*</span><span class="p">)(</span><span class="kt">int</span><span class="p">))</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">set</span><span class="p">,</span> <span class="s">"Set the pet's age"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"set"</span><span class="p">,</span> <span class="p">(</span><span class="kt">void</span> <span class="p">(</span><span class="n">Pet</span><span class="o">::*</span><span class="p">)(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="p">))</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">set</span><span class="p">,</span> <span class="s">"Set the pet's name"</span><span class="p">);</span>
</pre></div>
</div>
<p>The overload signatures are also visible in the method’s docstring:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="n">example</span><span class="o">.</span><span class="n">Pet</span><span class="p">)</span>
<span class="go">class Pet(__builtin__.object)</span>
<span class="go"> | Methods defined here:</span>
<span class="go"> |</span>
<span class="go"> | __init__(...)</span>
<span class="go"> | Signature : (Pet, str, int) -> NoneType</span>
<span class="go"> |</span>
<span class="go"> | set(...)</span>
<span class="go"> | 1. Signature : (Pet, int) -> NoneType</span>
<span class="go"> |</span>
<span class="go"> | Set the pet's age</span>
<span class="go"> |</span>
<span class="go"> | 2. Signature : (Pet, str) -> NoneType</span>
<span class="go"> |</span>
<span class="go"> | Set the pet's name</span>
</pre></div>
</div>
<p>If you have a C++14 compatible compiler <a class="footnote-reference" href="#cpp14" id="id3">[2]</a>, you can use an alternative
syntax to cast the overloaded function:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"set"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">overload_cast</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="p">(</span><span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">set</span><span class="p">),</span> <span class="s">"Set the pet's age"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"set"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">overload_cast</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&></span><span class="p">(</span><span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">set</span><span class="p">),</span> <span class="s">"Set the pet's name"</span><span class="p">);</span>
</pre></div>
</div>
<p>Here, <code class="docutils literal"><span class="pre">py::overload_cast</span></code> only requires the parameter types to be specified.
The return type and class are deduced. This avoids the additional noise of
<code class="docutils literal"><span class="pre">void</span> <span class="pre">(Pet::*)()</span></code> as seen in the raw cast. If a function is overloaded based
on constness, the <code class="docutils literal"><span class="pre">py::const_</span></code> tag should be used:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">Widget</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">float</span> <span class="n">y</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">float</span> <span class="n">y</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
<span class="p">};</span>
<span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Widget</span><span class="o">></span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Widget"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"foo_mutable"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">overload_cast</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">float</span><span class="o">></span><span class="p">(</span><span class="o">&</span><span class="n">Widget</span><span class="o">::</span><span class="n">foo</span><span class="p">))</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"foo_const"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">overload_cast</span><span class="o"><</span><span class="kt">int</span><span class="p">,</span> <span class="kt">float</span><span class="o">></span><span class="p">(</span><span class="o">&</span><span class="n">Widget</span><span class="o">::</span><span class="n">foo</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">const_</span><span class="p">));</span>
</pre></div>
</div>
<table class="docutils footnote" frame="void" id="cpp14" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[2]</a></td><td>A compiler which supports the <code class="docutils literal"><span class="pre">-std=c++14</span></code> flag
or Visual Studio 2015 Update 2 and newer.</td></tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">To define multiple overloaded constructors, simply declare one after the
other using the <code class="docutils literal"><span class="pre">.def(py::init<...>())</span></code> syntax. The existing machinery
for specifying keyword and default arguments also works.</p>
</div>
</div>
<div class="section" id="enumerations-and-internal-types">
<h2>Enumerations and internal types<a class="headerlink" href="#enumerations-and-internal-types" title="Permalink to this headline">¶</a></h2>
<p>Let’s now suppose that the example class contains an internal enumeration type,
e.g.:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">struct</span> <span class="n">Pet</span> <span class="p">{</span>
<span class="k">enum</span> <span class="n">Kind</span> <span class="p">{</span>
<span class="n">Dog</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
<span class="n">Cat</span>
<span class="p">};</span>
<span class="n">Pet</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="n">name</span><span class="p">,</span> <span class="n">Kind</span> <span class="n">type</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">type</span><span class="p">(</span><span class="n">type</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
<span class="n">Kind</span> <span class="n">type</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The binding code for this example looks as follows:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o"><</span><span class="n">Pet</span><span class="o">></span> <span class="n">pet</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">"Pet"</span><span class="p">);</span>
<span class="n">pet</span><span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o"><</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&</span><span class="p">,</span> <span class="n">Pet</span><span class="o">::</span><span class="n">Kind</span><span class="o">></span><span class="p">())</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"name"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">name</span><span class="p">)</span>
<span class="p">.</span><span class="n">def_readwrite</span><span class="p">(</span><span class="s">"type"</span><span class="p">,</span> <span class="o">&</span><span class="n">Pet</span><span class="o">::</span><span class="n">type</span><span class="p">);</span>
<span class="n">py</span><span class="o">::</span><span class="n">enum_</span><span class="o"><</span><span class="n">Pet</span><span class="o">::</span><span class="n">Kind</span><span class="o">></span><span class="p">(</span><span class="n">pet</span><span class="p">,</span> <span class="s">"Kind"</span><span class="p">)</span>
<span class="p">.</span><span class="n">value</span><span class="p">(</span><span class="s">"Dog"</span><span class="p">,</span> <span class="n">Pet</span><span class="o">::</span><span class="n">Kind</span><span class="o">::</span><span class="n">Dog</span><span class="p">)</span>
<span class="p">.</span><span class="n">value</span><span class="p">(</span><span class="s">"Cat"</span><span class="p">,</span> <span class="n">Pet</span><span class="o">::</span><span class="n">Kind</span><span class="o">::</span><span class="n">Cat</span><span class="p">)</span>
<span class="p">.</span><span class="n">export_values</span><span class="p">();</span>
</pre></div>
</div>
<p>To ensure that the <code class="docutils literal"><span class="pre">Kind</span></code> type is created within the scope of <code class="docutils literal"><span class="pre">Pet</span></code>, the
<code class="docutils literal"><span class="pre">pet</span></code> <code class="xref cpp cpp-class docutils literal"><span class="pre">class_</span></code> instance must be supplied to the <code class="xref cpp cpp-class docutils literal"><span class="pre">enum_</span></code>.
constructor. The <code class="xref cpp cpp-func docutils literal"><span class="pre">enum_::export_values()</span></code> function exports the enum entries
into the parent scope, which should be skipped for newer C++11-style strongly
typed enums.</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="n">Pet</span><span class="p">(</span><span class="s1">'Lucy'</span><span class="p">,</span> <span class="n">Pet</span><span class="o">.</span><span class="n">Cat</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">p</span><span class="o">.</span><span class="n">type</span>
<span class="go">Kind.Cat</span>
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">type</span><span class="p">)</span>
<span class="go">1L</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>When the special tag <code class="docutils literal"><span class="pre">py::arithmetic()</span></code> is specified to the <code class="docutils literal"><span class="pre">enum_</span></code>
constructor, pybind11 creates an enumeration that also supports rudimentary
arithmetic and bit-level operations like comparisons, and, or, xor, negation,
etc.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">enum_</span><span class="o"><</span><span class="n">Pet</span><span class="o">::</span><span class="n">Kind</span><span class="o">></span><span class="p">(</span><span class="n">pet</span><span class="p">,</span> <span class="s">"Kind"</span><span class="p">,</span> <span class="n">py</span><span class="o">::</span><span class="n">arithmetic</span><span class="p">())</span>
<span class="p">...</span>
</pre></div>
</div>
<p class="last">By default, these are omitted to conserve space.</p>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="compiling.html" class="btn btn-neutral float-right" title="Build systems" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="basics.html" class="btn btn-neutral" title="First steps" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2017, Wenzel Jakob.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'2.0.1',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|