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;
This does not compile with a huge compiler dump. How do I do this?
Boost.Python doesn't come with an operator<<(ostream&, const object&)
but we can write our own to mimic what Python would do natively: 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))();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With