|
Packit |
534379 |
![pybind11 logo](https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png)
|
|
Packit |
534379 |
|
|
Packit |
534379 |
# pybind11 — Seamless operability between C++11 and Python
|
|
Packit |
534379 |
|
|
Packit |
534379 |
[![Documentation Status](https://readthedocs.org/projects/pybind11/badge/?version=master)](http://pybind11.readthedocs.org/en/master/?badge=master)
|
|
Packit |
534379 |
[![Documentation Status](https://readthedocs.org/projects/pybind11/badge/?version=stable)](http://pybind11.readthedocs.org/en/stable/?badge=stable)
|
|
Packit |
534379 |
[![Gitter chat](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/pybind/Lobby)
|
|
Packit |
534379 |
[![Build Status](https://travis-ci.org/pybind/pybind11.svg?branch=master)](https://travis-ci.org/pybind/pybind11)
|
|
Packit |
534379 |
[![Build status](https://ci.appveyor.com/api/projects/status/riaj54pn4h08xy40?svg=true)](https://ci.appveyor.com/project/wjakob/pybind11)
|
|
Packit |
534379 |
|
|
Packit |
534379 |
**pybind11** is a lightweight header-only library that exposes C++ types in Python
|
|
Packit |
534379 |
and vice versa, mainly to create Python bindings of existing C++ code. Its
|
|
Packit |
534379 |
goals and syntax are similar to the excellent
|
|
Packit |
534379 |
[Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library
|
|
Packit |
534379 |
by David Abrahams: to minimize boilerplate code in traditional extension
|
|
Packit |
534379 |
modules by inferring type information using compile-time introspection.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
The main issue with Boost.Python—and the reason for creating such a similar
|
|
Packit |
534379 |
project—is Boost. Boost is an enormously large and complex suite of utility
|
|
Packit |
534379 |
libraries that works with almost every C++ compiler in existence. This
|
|
Packit |
534379 |
compatibility has its cost: arcane template tricks and workarounds are
|
|
Packit |
534379 |
necessary to support the oldest and buggiest of compiler specimens. Now that
|
|
Packit |
534379 |
C++11-compatible compilers are widely available, this heavy machinery has
|
|
Packit |
534379 |
become an excessively large and unnecessary dependency.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Think of this library as a tiny self-contained version of Boost.Python with
|
|
Packit |
534379 |
everything stripped away that isn't relevant for binding generation. Without
|
|
Packit |
534379 |
comments, the core header files only require ~4K lines of code and depend on
|
|
Packit |
534379 |
Python (2.7 or 3.x, or PyPy2.7 >= 5.7) and the C++ standard library. This
|
|
Packit |
534379 |
compact implementation was possible thanks to some of the new C++11 language
|
|
Packit |
534379 |
features (specifically: tuples, lambda functions and variadic templates). Since
|
|
Packit |
534379 |
its creation, this library has grown beyond Boost.Python in many ways, leading
|
|
Packit |
534379 |
to dramatically simpler binding code in many common situations.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Tutorial and reference documentation is provided at
|
|
Packit |
534379 |
[http://pybind11.readthedocs.org/en/master](http://pybind11.readthedocs.org/en/master).
|
|
Packit |
534379 |
A PDF version of the manual is available
|
|
Packit |
534379 |
[here](https://media.readthedocs.org/pdf/pybind11/master/pybind11.pdf).
|
|
Packit |
534379 |
|
|
Packit |
534379 |
## Core features
|
|
Packit |
534379 |
pybind11 can map the following core C++ features to Python
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Functions accepting and returning custom data structures per value, reference, or pointer
|
|
Packit |
534379 |
- Instance methods and static methods
|
|
Packit |
534379 |
- Overloaded functions
|
|
Packit |
534379 |
- Instance attributes and static attributes
|
|
Packit |
534379 |
- Arbitrary exception types
|
|
Packit |
534379 |
- Enumerations
|
|
Packit |
534379 |
- Callbacks
|
|
Packit |
534379 |
- Iterators and ranges
|
|
Packit |
534379 |
- Custom operators
|
|
Packit |
534379 |
- Single and multiple inheritance
|
|
Packit |
534379 |
- STL data structures
|
|
Packit |
534379 |
- Smart pointers with reference counting like ``std::shared_ptr``
|
|
Packit |
534379 |
- Internal references with correct reference counting
|
|
Packit |
534379 |
- C++ classes with virtual (and pure virtual) methods can be extended in Python
|
|
Packit |
534379 |
|
|
Packit |
534379 |
## Goodies
|
|
Packit |
534379 |
In addition to the core functionality, pybind11 provides some extra goodies:
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Python 2.7, 3.x, and PyPy (PyPy2.7 >= 5.7) are supported with an
|
|
Packit |
534379 |
implementation-agnostic interface.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- It is possible to bind C++11 lambda functions with captured variables. The
|
|
Packit |
534379 |
lambda capture data is stored inside the resulting Python function object.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- pybind11 uses C++11 move constructors and move assignment operators whenever
|
|
Packit |
534379 |
possible to efficiently transfer custom data types.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- It's easy to expose the internal storage of custom data types through
|
|
Packit |
534379 |
Pythons' buffer protocols. This is handy e.g. for fast conversion between
|
|
Packit |
534379 |
C++ matrix classes like Eigen and NumPy without expensive copy operations.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- pybind11 can automatically vectorize functions so that they are transparently
|
|
Packit |
534379 |
applied to all entries of one or more NumPy array arguments.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Python's slice-based access and assignment operations can be supported with
|
|
Packit |
534379 |
just a few lines of code.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Everything is contained in just a few header files; there is no need to link
|
|
Packit |
534379 |
against any additional libraries.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Binaries are generally smaller by a factor of at least 2 compared to
|
|
Packit |
534379 |
equivalent bindings generated by Boost.Python. A recent pybind11 conversion
|
|
Packit |
534379 |
of PyRosetta, an enormous Boost.Python binding project,
|
|
Packit |
534379 |
[reported](http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf) a binary
|
|
Packit |
534379 |
size reduction of **5.4x** and compile time reduction by **5.8x**.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- Function signatures are precomputed at compile time (using ``constexpr``),
|
|
Packit |
534379 |
leading to smaller binaries.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
- With little extra effort, C++ types can be pickled and unpickled similar to
|
|
Packit |
534379 |
regular Python objects.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
## Supported compilers
|
|
Packit |
534379 |
|
|
Packit |
534379 |
1. Clang/LLVM 3.3 or newer (for Apple Xcode's clang, this is 5.0.0 or newer)
|
|
Packit |
534379 |
2. GCC 4.8 or newer
|
|
Packit |
534379 |
3. Microsoft Visual Studio 2015 Update 3 or newer
|
|
Packit |
534379 |
4. Intel C++ compiler 17 or newer (16 with pybind11 v2.0 and 15 with pybind11 v2.0 and a [workaround](https://github.com/pybind/pybind11/issues/276))
|
|
Packit |
534379 |
5. Cygwin/GCC (tested on 2.5.1)
|
|
Packit |
534379 |
|
|
Packit |
534379 |
## About
|
|
Packit |
534379 |
|
|
Packit |
534379 |
This project was created by [Wenzel Jakob](http://rgl.epfl.ch/people/wjakob).
|
|
Packit |
534379 |
Significant features and/or improvements to the code were contributed by
|
|
Packit |
534379 |
Jonas Adler,
|
|
Packit |
534379 |
Lori A. Burns,
|
|
Packit |
534379 |
Sylvain Corlay,
|
|
Packit |
534379 |
Trent Houliston,
|
|
Packit |
534379 |
Axel Huebl,
|
|
Packit |
534379 |
@hulucc,
|
|
Packit |
534379 |
Sergey Lyskov
|
|
Packit |
534379 |
Johan Mabille,
|
|
Packit |
534379 |
Tomasz Miąsko,
|
|
Packit |
534379 |
Dean Moldovan,
|
|
Packit |
534379 |
Ben Pritchard,
|
|
Packit |
534379 |
Jason Rhinelander,
|
|
Packit |
534379 |
Boris Schäling,
|
|
Packit |
534379 |
Pim Schellart,
|
|
Packit |
534379 |
Henry Schreiner,
|
|
Packit |
534379 |
Ivan Smirnov, and
|
|
Packit |
534379 |
Patrick Stewart.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
### License
|
|
Packit |
534379 |
|
|
Packit |
534379 |
pybind11 is provided under a BSD-style license that can be found in the
|
|
Packit |
534379 |
``LICENSE`` file. By using, distributing, or contributing to this project,
|
|
Packit |
534379 |
you agree to the terms and conditions of this license.
|