[Numpy-discussion] Add pybind11 to docs about writing binding code

Hans Dembinski hans.dembinski at gmail.com
Wed Aug 15 05:40:23 EDT 2018


Dear all,

I just joined the numpy mailing list to suggest an enhancement of the docs about writing binding code

https://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html <https://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html>

I hope this is the right place to discuss this.

So, this page is one of the first hits in Google when you search for "python bindings numpy". It is an important page for orientation. What I miss is a longer article about pybind11.

http://pybind11.readthedocs.io/en/stable/ <http://pybind11.readthedocs.io/en/stable/>
https://github.com/pybind/pybind11 <https://github.com/pybind/pybind11>

pybind11 is currently the best tool on the market to wrap C++ code to Python. This is my professional opinion. When you look at the facts, it is hard to disagree. Pybind11 is based on the approach of Boost.Python, but is a compact project that doesn't require Boost and is developed independently. If you use it in a Python package, you can add it as a requirement and pip will happily install it. It doesn't require you to learn a new language, the bindings are generated using C++ meta-programming techniques under the hood. pybind11 has outstanding documentation and is extremely popular on github (3800+ stars). Refcounting is done automatically, which is why I would even use it to wrap C code. Naturally, it has excellent support for numpy arrays. Pybind11 is FOSS (BSD-style license).

I have used Cython, Boost.Python, SWIG, and pybind11 in small to large projects, and pybind11 is by far the most pleasant and the most powerful. You can do really sophisticated things in pybind11, which I cannot imagine doing with other binding tools, and most importantly, it never chokes over your C++ code. Cython and SWIG both have trouble with certain C++ idioms, which is not surprising because C++ is notoriously difficult to parse and these tools were primarily developed to wrap C (which is much easier to parse). For C++, it is much better to not add a custom parser to the toolchain and just let the C++ compiler generate the low-level binding code. This is what pybind11 does.

So far all these reasons and more, it should be mentioned and even highlighted here:

https://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html <https://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html>

I am happy to write a section about it. Disclaimer: I am not at all affiliated with the pybind11 developers, just a thankful user.

Best regards,
Hans
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180815/6ae5ebd7/attachment-0001.html>


More information about the NumPy-Discussion mailing list