blob: 08986056929fa45c2ce5a137b8a9e2ac284803dd [file] [log] [blame] [view]
Wenzel Jakob10d992e2015-08-04 13:59:51 +02001![pybind11 logo](https://github.com/wjakob/pybind11/raw/master/logo.png)
2
3# pybind11 — Seamless operability between C++11 and Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob3b806d42015-10-11 16:29:35 +02005[![Build Status](https://travis-ci.org/wjakob/pybind11.svg?branch=master)](https://travis-ci.org/wjakob/pybind11)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02006[![Documentation Status](https://readthedocs.org/projects/pybind11/badge/?version=latest)](http://pybind11.readthedocs.org/en/latest/?badge=latest)
Wenzel Jakob3b806d42015-10-11 16:29:35 +02007
Wenzel Jakob7641c1d2015-10-18 14:48:24 +02008**pybind11** is a lightweight header-only library that exposes C++ types in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +02009and vice versa, mainly to create Python bindings of existing C++ code. Its
10goals and syntax are similar to the excellent
11[Boost.Python](http://www.boost.org/doc/libs/1_58_0/libs/python/doc/) library
12by David Abrahams: to minimize boilerplate code in traditional extension
13modules by inferring type information using compile-time introspection.
14
15The main issue with Boost.Pythonand the reason for creating such a similar
16projectis Boost. Boost is an enormously large and complex suite of utility
17libraries that works with almost every C++ compiler in existence. This
18compatibility has its cost: arcane template tricks and workarounds are
19necessary to support the oldest and buggiest of compiler specimens. Now that
20C++11-compatible compilers are widely available, this heavy machinery has
21become an excessively large and unnecessary dependency.
22
23Think of this library as a tiny self-contained version of Boost.Python with
Wenzel Jakob7641c1d2015-10-18 14:48:24 +020024everything stripped away that isn't relevant for binding generation. The core
25header files only require ~2K lines of code and depend on Python (2.7 or 3.x)
26and the C++ standard library. This compact implementation was possible thanks
27to some of the new C++11 language features (tuples, lambda functions and
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020028variadic templates). Since its creation, this library has grown beyond
29Boost.Python in many ways, leading to dramatically simpler binding code in many
30common situations.
31
32Tutorial and reference documentation is provided at
33[http://pybind11.readthedocs.org/en/latest](http://pybind11.readthedocs.org/en/latest).
Wenzel Jakob38bd7112015-07-05 20:05:44 +020034
35## Core features
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020036pybind11 can map the following core C++ features to Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020037
38- Functions accepting and returning custom data structures per value, reference, or pointer
39- Instance methods and static methods
40- Overloaded functions
41- Instance attributes and static attributes
42- Exceptions
43- Enumerations
44- Callbacks
45- Custom operators
46- STL data structures
47- Smart pointers with reference counting like `std::shared_ptr`
48- Internal references with correct reference counting
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020049- C++ classes with virtual (and pure virtual) methods can be extended in Python
Wenzel Jakob38bd7112015-07-05 20:05:44 +020050
51## Goodies
52In addition to the core functionality, pybind11 provides some extra goodies:
53
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020054- pybind11 uses C++11 move constructors and move assignment operators whenever
55 possible to efficiently transfer custom data types.
56
57- It is possible to bind C++11 lambda functions with captured variables. The
58 lambda capture data is stored inside the resulting Python function object.
59
Wenzel Jakob38bd7112015-07-05 20:05:44 +020060- 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
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020064- pybind11 can automatically vectorize functions so that they are transparently
65 applied to all entries of one or more NumPy array arguments.
66
Wenzel Jakob38bd7112015-07-05 20:05:44 +020067- Python's slice-based access and assignment operations can be supported with
68 just a few lines of code.
69
Wenzel Jakob7641c1d2015-10-18 14:48:24 +020070- Everything is contained in just a few header files; there no need to link
71 against any additional libraries.