Printing Boost Python object
I have a Boost Python object
py::object obj = whatever();
I want to print it using normal python rules.
// I want the effect of print 'My object is ', obj
std::cout << "My object is " << obj << std::endl;
It doesn't compile with a huge compiler dump. How to do it?
+3
Barry
source
to share
1 answer
Boost.Python does not ship with operator<<(ostream&, const object&)
, but we can write our own to simulate what Python would do initially: call str
:
namespace py = boost::python;
std::ostream& operator<<(std::ostream& os, const py::object& o)
{
return os << py::extract<std::string>(py::str(o))();
}
+6
Barry
source
to share