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

David Abrahams david.abrahams at rcn.com
Fri May 31 23:16:39 CEST 2002


From: "Pearu Peterson" <pearu at cens.ioc.ee>

>
> Thanks, though I don't understand how the above needs not to depend on
the
> class, A,

It does, in a way. obj->ob_type is the class. All extension classes are
instances of a common metaclass.

Oh, you want to find out if the object is an instance of a /particular/
wrapped class!

> 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);
>   ...
> }

That's probably not quite right. Since rvalues bind to const references,
all that proves is that there is a way to convert obj to an object of type
A. For example, there might be an implicit conversion from B->A.

However, if you use from_python<A const*>, you're guaranteed that obj
contains an actual A instance if convertible() succeeds.

-Dave







More information about the Cplusplus-sig mailing list