2

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.