Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pybind11 equivalent of boost::python::extract?

I am considering port of a complex code from boost::python to pybind11, but I am puzzled by the absence of something like boost::python::extract<...>().check(). I read pybind11::cast<T> can be used to extract c++ object from a py::object, but the only way to check if the cast is possible is by calling it and catching the exception when the cast fails. Is there something I am overlooking?

like image 225
eudoxos Avatar asked Nov 04 '16 13:11

eudoxos


1 Answers

isinstance will do the job (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))  
{
   ...
}  
like image 136
Malick Avatar answered Oct 19 '22 12:10

Malick