Boost logo

Boost :

From: Anton Gluck (gluc_at_[hidden])
Date: 2000-11-27 01:04:46


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

This uses the built-in repr() for extensionclass, 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, where
each object is of type ClusterVar, and not of type CatVar or ContVar
anymore. Somewhere along the way I lost that information. Once I figure
out how to get back the original type, I hope the above code will do the
trick for me.

Thanks,

Toni


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