How to tell an object's class?

Edward K. Ream edreamleo at charter.net
Fri Feb 24 09:12:58 EST 2006


 Hi,



I would like to create a dictionary of all the classes that exist while my 
app is running.  This is the best I can do:



def defineClassesDict (self):



    self.allClassesDict = {}



    for z in gc.get_objects():

        t = type(z)

        if t == types.ClassType:

            name = z.__name__

        elif t == types.InstanceType:

            name = z.__class__.__name__

        elif repr(t).startswith('<class'): # A wretched kludge.

            name = z.__class__.__name__

        elif t == types.TypeType:

            name =z.__name__

        else:

            name = None

        if name:

            self.allClassesDict [name] = z



This works, but it relies on a wretched (and slow) kludge, namely the test:



repr(t).startswith('<class')



There seems to be a hole in the types module.  Can anyone suggest a better 
approach?  Thanks.



Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------





More information about the Python-list mailing list