Finding all classes in a module

Hans Nowak wurmy at earthlink.net
Fri Aug 9 15:04:03 EDT 2002


Tom Harris wrote:
> If I import a module, how do I find the names of the class objects it
> contains?

The replies you got so far only seem to work for old-style classes:

 >>> import types
 >>> class D(dict): pass

 >>> isinstance(D, types.ClassType)
0

I think this should work for new-style classes, though:

 >>> isinstance(D, type)
1

So, to find both types of classes, do something like

   if isinstance(obj, types.ClassType) or isinstance(obj, type):
       ...

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list