[C++-sig] V2: isinstance(obj,cls) equivalent

Pearu Peterson pearu at cens.ioc.ee
Fri May 31 22:23:51 CEST 2002


On Fri, 31 May 2002, David Abrahams wrote:

> 
> From: "Pearu Peterson" <pearu at cens.ioc.ee>
> 
> > What would be the best way to determine if
> >
> >   PyObject *obj
> >
> > is an instance of a Boost.Python V2 wrapped class, say A?
> 
> The library does it by checking its metaclass. That is, does
> 
>     obj->ob_type->ob_type == boost::python::objects::class_metatype().get()

Thanks, though I don't understand how the above needs not to depend on the
class, A, but since I also need to extract the corresponding instance from
obj, I use from_python facility:

const A pyobj_to_A(PyObject* obj) {
  boost::python::from_python<const A &> c(obj);
  if (c.convertible()) return c(obj);
  ...
}

Pearu






More information about the Cplusplus-sig mailing list