Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-11-27 01:23:04


----- Original Message -----
From: "Anton Gluck" <gluc_at_[hidden]>

> Dave,
>
> > I suggest writing isinstance() in Python something like this.
> >
> > # untested! (not needed For Python 2.0)
> > def _is_subclass(t1, t2):
> > for b in getattr(t1. '__bases__', ()):
> > if b is t2 or _is_subclass(b, t2):
> > return 1
> > return 0
> >
> > # untested! For Python 1.5 or 2.0
> > def is_instance(obj, type):
> > return isinstance(obj, type) or _is_subclass(getattr(obj,
'__class__',
> > None), type)
>
> I tried my luck with a very simple version of a custom isinstance(). I
> wrote it like this:
>
> def _is_instance(object, extensionclass):
> if object.__class__ == extensionclass:
> return 1
> else:
> return 0

You are indeed rolling the dice here, Toni. Your technique won't work
because it never checks the __bases__ of an object's class. You need to walk
the inheritance hierarchy until you find the desired class, which is what my
version is supposed to do.

> This uses the built-in repr() for extensionclass

Do you really mean repr()? I don't see that anywhere, and it wouldn't be
likely to help anyway.

> , which will match an
> object's __class__ if it is an instance of that extensionclass. This
> worked for simple test cases, but unfortunately not for the purpose I
> needed this for. My problem is that I have a vector of ClusterVars

Really, ClusterVars? or are they ClusterVar*s?

>, where
> each object is of type ClusterVar, and not of type CatVar or ContVar
> anymore. Somewhere along the way I lost that information.

If ClusterVar doesn't have a virtual function or if your vector really
contains ClusterVar by value, that information is lost forever. Otherwise,
have a look at the section on Reflecting C++ Inheritance Relationships in
the documentation; it should help.

-Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk