boost-python – pybind11相当于boost :: python :: extract?

我正在考虑从boost ::
python到pybind11的复杂代码的端口,但我对像缺少像boost :: python :: extract< …>().check()这样的东西感到困惑.我读了pybind11 :: cast< T>可以用来从py :: object中提取c对象,但检查是否可以进行强制转换的唯一方法是调用它并在强制转换失败时捕获异常.有什么我可以忽略的吗? 最佳答案 isintance将完成这项工作(
doc):

namespace py = pybind11;
py::object  obj =  ...
if (py::isinstance<py::array_t<double>>(obj))
{
    ....
} 
else if (py::isinstance<py::str>(obj))
{
   std::string val = obj.cast<std::string>();
   std::cout << val  << std::endl;
} 
else if (py::isinstance<py::list>(obj))  
{
   ...
}  
点赞