How to acces attributes of a structure using pybind?

Viewed 1817

I'm using pybind to wrap some C++ functions and then use it in Python. I need some structures but I don't know how to access in Python to its attributes. My structures don't have methods only attributes so I think the binding is like this (maybe this is also wrong):

py::class_<Struct_Sample>(m, "Struct_Sample");

And this is the struct:

typedef struct Struct_Sample
{
   float time_ms;
   float frequency_mhz;
} Struct_Sample;

How can I access to the attributes in python?

1 Answers

According to the docs you can use def_readwrite:

py::class_<Struct_Sample>(m, "Struct_Sample")
    .def_readwrite("time_ms", &Struct_Sample::time_ms);
Related