Re: Finding out an objects type (AND Re: Free Python Software)

Jeff P. Lankford (jpl@nrtc.northrop.com)
Wed, 31 Aug 1994 15:45:10 GMT

In article <9408292307.AA24664@dvts.com>, powers@dvts.com (Michael Powers) writes:
|>
|> In all this time I have not had a need for this but now I found one. I want
|> to know what class and object is. For instance I would like to do this:
|>
|> class Fake:
|> fakevar = 9
|>
|> f = Fake()
|> if f isKindof Fake:
|> print "Found it"
|>
|> type doesn't do this for me, it only says whether its and instance, a
|> string, a class, etc.

Note that your request is different from your example, neither of which have
anything to do with type(). In your request, you ask for a mechanism for
obtaining the class of an object. In your example, you use a predicate that
compares an instance with a class for class membership. Types have nothing
to do with class heirarchy.

Following your example (but satisfying your request), try:
if f.__class__ == Fake:
...
or even
if f.__class__.__name__ == Fake.__name__:
...
You can imagine how trivial it would be to compose the function
isInstanceOfClass(class_instance, class), as your example conjectures.
This is marginally documented in both the Reference Manual and the
Library Reference -- RTFM.

On the other hand, if you're really interested in types, refer to the
typep.py module i distributed to the net yesterday. Incidentally, i
added a typeof function to return the name of the type of the object
argument (loosely analogous to Common Lisp). E-mail me directly if you
want an updated release.
jpl