pybind_struct is a single header include that defines macros for exporting aggregate classes when using pybind11. This reduces the amount of boilerplate necessary when exposing simple structs to Python.
For example, consider the following struct:
struct example
{
int a;
float b;
std::vector<double> c;
};
The macro PYBIND_STRUCT_READWRITE(example, a, b, c)
is equivalent to:
py::class_<example>(m, "example")
.def(py::init<>())
.def(py::init<int, float, std::vector<double>>(), py::arg("a")=int{}, py::arg("b")=float{}, py::arg("c")=std::vector<double>{})
// + Other Python magic functions, listed below
.def_readwrite("a", &example::a)
.def_readwrite("b", &example::b)
.def_readwrite("c", &example::c);
There also exists PYBIND_STRUCT_READONLY
which defines no constructors and read-only access to its members, and PYBIND_STRUCT_ENUM
for defining enums in a similar fashion.
pybind_struct will also define magic methods on your type:
- Pickle serialization and deserialization support
__copy__
and__deepcopy__
__str__
ifstd::ostream << Type
is valid__nonzero__ / __bool__
(depending on Python 2/3) ifstd::is_convertible<Type, bool>
is true__eq__
,__ne__
,__lt__
,__le__
,__gt__
and__ge__
ifType OPERATOR Type
is valid for the corresponding operator
pybind_struct requires a C++17 compliant compiler.
To use pybind_struct, simply include the single header file:
#include "pybind_struct.hpp"