gpt4 book ai didi

python - 打印 Boost Python 对象

转载 作者:太空狗 更新时间:2023-10-29 22:22:14 25 4
gpt4 key购买 nike

我有一个 Boost Python 对象

py::object obj = whatever();

我想使用普通的 python 规则打印它。

// I want the effect of print 'My object is ', obj
std::cout << "My object is " << obj << std::endl;

这不会使用巨大的编译器转储进行编译。我该怎么做?

最佳答案

Boost.Python 没有 operator<<(ostream&, const object&)但我们可以编写自己的代码来模仿 Python 的 native 功能:调用 str :

namespace py = boost::python;

std::ostream& operator<<(std::ostream& os, const py::object& o)
{
return os << py::extract<std::string>(py::str(o))();
}

关于python - 打印 Boost Python 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com