c – 查找boost python对象的类型

前端之家收集整理的这篇文章主要介绍了c – 查找boost python对象的类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经将 python嵌入到c中,我想知道是否有办法找到
boost :: python :: object的类型是执行python模块的函数后的结果.我有这样的代码
  1. boost::python::object module_ = boost::python::import("..libName");
  2. boost::python::object result_ = module_.attr("..functionName")(arg1,arg2,...);
  3. //suppose if the result is int,int a_ = boost::python::extract<int>(result_);

从上面的代码片段,我想知道的是如果有办法找到类型
提取之前的结果.在上面的代码中,result_可以是任何类型,如list,tuple …

解决方法

你可以试试看
  1. std::vector<std::string> list_to_vector(boost::python::list& l)
  2. {
  3. for (int i = 0; i < len(n); ++i)
  4. {
  5. boost::python::extract<boost::python::object> objectExtractor(l[i]);
  6. boost::python::object o=objectExtractor();
  7. std::string object_classname = boost::python::extract<std::string>(o.attr("__class__").attr("__name__"));
  8. std::cout<<"this is an Object: "<<object_classname<<std::endl;
  9. }
  10. ...................................................
  11. ...................................................
  12. }

它适用于我

猜你在找的C&C++相关文章