A python extension module written in C++ with pybind11 bindings, containing implementations of various constructs, including:
- generators, including
send
,close
, andthrow
methods -
__init_subclass__
- using it to register subclasses - decorators, simple and parameterised
- a context manager
- some prime number stuff, for performance comparison aginst equivalent python and rust implementations.
- polymorphism across C++ and python (subclassing C++ classes in python, calling python overrides through a C++ interface)
- dynamic immutable values wrapped in a
Constants
singleton - C and C++ enumerations
- vectorised functions with a performance comparison against python and vectorised numpy
- type annotations for C++ implementations
Most of the code originated in this project which was originally intended to demonstate how to make pybind11 work with poetry. The project evolved and became more about:
- how to implement certain python constructs in C++
- performance comparison between python modules implemented in python, rust and C++
Build and install the package:
pip install -e .[dev]
Test:
pytest
Performance (vectorisation comparison):
python test/test_vectorised.py
Use, e.g.:
from pybind11_examples import FibGenerator, Collatz
fg = FibGenerator()
print([next(fg) for _ in range(10)])
print(list(Collatz(19)))
Type stubs are generated based on the signatures and docstrings in module.cpp, using the pybind11-stubgen
package directly on the C++ shared object, like so:
pybind11-stubgen _pybind11_examples