From: Anton Gluck (gluc_at_[hidden])
Date: 2000-11-27 02:00:08
> > > 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,
> > > 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.
In my case I am looking for an exact match between the object's class and
whatever class I am checking against. I happen to know ahead of time that
there are no further subclasses since this is the inheritance:
Still, do you think my approach is too risky?
> > 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.
In the comparison "obj.__class__ == extclass" string representations of
the two operands are being compared - if I understand the Python reference
correctly. This would use repr() implicitly - but maybe I'm just falling
off the planet here. These late night sessions are getting at me...
> > , 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?
You are right, they are ClusterVar*s. But since I am using the iterator we
discussed earlier to get to the vector elements, I am being given
ClusterVars. Therefore, for all practical purposes, it might as well be a
vector of ClusterVars. (The return type of getItem(), which retrieves
vector elements, is ClusterVar. I have tried casting the ClusterVar to
either CatVar or ContVar, but got errors like 'cannot cast from ClusterVar
to CatVar'. I think this is where I need to look for a fix. Now that I
think of it: perhaps I can rewrite getItem() to return ClusterVar*s. That
> >, 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.
ClusterVar does have a virtual function that ought to give me information
about whether I am dealing with a CatVar or a ContVar, but when I called
it on the returned objects it just says "unknown type" (there is an
enumeration with values for CatVar, ContVar, and unknown). This made me
think that something else is wrong here as well, since this ought not to
Thanks for your help,
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk