getting class information from module name

holger krekel pyth at devel.trillke.net
Tue Aug 20 21:47:55 CEST 2002


sameer wrote:
> I would like to instantiate instances of all the classes that I have
> defined in a module.  I know the module name, how do I go about with
> the instantiation part?

you could do something like:

instances = []
for modobject in somemodule.__dict__.values():
    try:
        instances.append(modobject())  # note the constructor call
    except:
        pass
       
but this probably only works for special modules as some classes
require some other objects to be instantiated. 

regards,

    holger




More information about the Python-list mailing list