Reputation: 1373
Let's say one has such a class
class Data
{
public:
Data(double d): value_(d) {};
private:
double value_;
};
Is it possible to export it in Python with pybind11 such that
d = Data(3.14)
print(d)
displays 3.14 instead of something like
Data object at 0x7fed8a8c3298
Upvotes: 2
Views: 1548
Reputation: 13599
You can do something like this when you export:
class_<Data>("Data", module)
.def("__repr__", [](const Data& d){ return std::to_string(d.getValue()); });
Notice I added that getValue
method since value_
is private. Though depending on your interface, it might make more sense to add something like Data::toString()
instead.
http://pybind11.readthedocs.io/en/stable/classes.html#binding-lambda-functions
Upvotes: 3