Trying to generate a list of the subclasses of C

Duncan Booth duncan.booth at invalid.invalid
Mon Jan 16 17:31:54 CET 2006


Charles Krug wrote:

> The end result I'm after is an automatically generated dictionary
> containing instaces of the subclasses keyed by the subclass names:
> 
> {'D':D(), 'E':E(), . . . }
> 
> I can see the information I need in the module's __dict__ and by using
> the dir() method, but I'm not having much success extracting it.

Try this:

class C(object): pass

class D(C): pass

class E(C): pass

def CSubclasses():
    return dict((cls.__name__, cls) for cls in C.__subclasses__())

print CSubclasses()



More information about the Python-list mailing list