intro.rst revision 11986:c12e4625ab56
16882SBrad.Beckmann@amd.com.. image:: pybind11-logo.png 26882SBrad.Beckmann@amd.com 36882SBrad.Beckmann@amd.comAbout this project 46882SBrad.Beckmann@amd.com================== 56882SBrad.Beckmann@amd.com**pybind11** is a lightweight header-only library that exposes C++ types in Python 66882SBrad.Beckmann@amd.comand vice versa, mainly to create Python bindings of existing C++ code. Its 76882SBrad.Beckmann@amd.comgoals and syntax are similar to the excellent `Boost.Python`_ library by David 86882SBrad.Beckmann@amd.comAbrahams: to minimize boilerplate code in traditional extension modules by 96882SBrad.Beckmann@amd.cominferring type information using compile-time introspection. 106882SBrad.Beckmann@amd.com 116882SBrad.Beckmann@amd.com.. _Boost.Python: http://www.boost.org/doc/libs/release/libs/python/doc/index.html 126882SBrad.Beckmann@amd.com 136882SBrad.Beckmann@amd.comThe main issue with Boost.Python—and the reason for creating such a similar 146882SBrad.Beckmann@amd.comproject—is Boost. Boost is an enormously large and complex suite of utility 156882SBrad.Beckmann@amd.comlibraries that works with almost every C++ compiler in existence. This 166882SBrad.Beckmann@amd.comcompatibility has its cost: arcane template tricks and workarounds are 176882SBrad.Beckmann@amd.comnecessary to support the oldest and buggiest of compiler specimens. Now that 186882SBrad.Beckmann@amd.comC++11-compatible compilers are widely available, this heavy machinery has 196882SBrad.Beckmann@amd.combecome an excessively large and unnecessary dependency. 206882SBrad.Beckmann@amd.com 216882SBrad.Beckmann@amd.comThink of this library as a tiny self-contained version of Boost.Python with 226882SBrad.Beckmann@amd.comeverything stripped away that isn't relevant for binding generation. Without 236882SBrad.Beckmann@amd.comcomments, the core header files only require ~2.5K lines of code and depend on 246882SBrad.Beckmann@amd.comPython (2.7 or 3.x) and the C++ standard library. This compact implementation 256882SBrad.Beckmann@amd.comwas possible thanks to some of the new C++11 language features (specifically: 266882SBrad.Beckmann@amd.comtuples, lambda functions and variadic templates). Since its creation, this 276882SBrad.Beckmann@amd.comlibrary has grown beyond Boost.Python in many ways, leading to dramatically 286882SBrad.Beckmann@amd.comsimpler binding code in many common situations. 297039Snate@binkert.org 307039Snate@binkert.orgCore features 316882SBrad.Beckmann@amd.com************* 327039Snate@binkert.orgThe following core C++ features can be mapped to Python 336882SBrad.Beckmann@amd.com 346882SBrad.Beckmann@amd.com- Functions accepting and returning custom data structures per value, reference, or pointer 356882SBrad.Beckmann@amd.com- Instance methods and static methods 366882SBrad.Beckmann@amd.com- Overloaded functions 376882SBrad.Beckmann@amd.com- Instance attributes and static attributes 387039Snate@binkert.org- Arbitrary exception types 397039Snate@binkert.org- Enumerations 407039Snate@binkert.org- Callbacks 417039Snate@binkert.org- Iterators and ranges 427039Snate@binkert.org- Custom operators 436882SBrad.Beckmann@amd.com- Single and multiple inheritance 447039Snate@binkert.org- STL data structures 457039Snate@binkert.org- Iterators and ranges 467039Snate@binkert.org- Smart pointers with reference counting like ``std::shared_ptr`` 476882SBrad.Beckmann@amd.com- Internal references with correct reference counting 487039Snate@binkert.org- C++ classes with virtual (and pure virtual) methods can be extended in Python 496882SBrad.Beckmann@amd.com 506882SBrad.Beckmann@amd.comGoodies 517039Snate@binkert.org******* 527039Snate@binkert.orgIn addition to the core functionality, pybind11 provides some extra goodies: 536882SBrad.Beckmann@amd.com 547039Snate@binkert.org- It is possible to bind C++11 lambda functions with captured variables. The 557039Snate@binkert.org lambda capture data is stored inside the resulting Python function object. 567039Snate@binkert.org 576882SBrad.Beckmann@amd.com- pybind11 uses C++11 move constructors and move assignment operators whenever 586882SBrad.Beckmann@amd.com possible to efficiently transfer custom data types. 597039Snate@binkert.org 606882SBrad.Beckmann@amd.com- It's easy to expose the internal storage of custom data types through 61 Pythons' buffer protocols. This is handy e.g. for fast conversion between 62 C++ matrix classes like Eigen and NumPy without expensive copy operations. 63 64- pybind11 can automatically vectorize functions so that they are transparently 65 applied to all entries of one or more NumPy array arguments. 66 67- Python's slice-based access and assignment operations can be supported with 68 just a few lines of code. 69 70- Everything is contained in just a few header files; there is no need to link 71 against any additional libraries. 72 73- Binaries are generally smaller by a factor of at least 2 compared to 74 equivalent bindings generated by Boost.Python. A recent pybind11 conversion 75 of `PyRosetta`_, an enormous Boost.Python binding project, reported a binary 76 size reduction of **5.4x** and compile time reduction by **5.8x**. 77 78- When supported by the compiler, two new C++14 features (relaxed constexpr and 79 return value deduction) are used to precompute function signatures at compile 80 time, leading to smaller binaries. 81 82- With little extra effort, C++ types can be pickled and unpickled similar to 83 regular Python objects. 84 85.. _PyRosetta: http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf 86 87Supported compilers 88******************* 89 901. Clang/LLVM (any non-ancient version with C++11 support) 912. GCC (any non-ancient version with C++11 support) 923. Microsoft Visual Studio 2015 or newer 934. Intel C++ compiler v15 or newer 94