How to tell an object's class?

E

Edward K. Ream

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top