Factory function to generate a named class

Derek Fountain nomail at hursley.ibm.com
Tue Sep 23 13:20:35 CEST 2003


I have a situation where I have, in a string, the name of a class I want an
instance of. I've written a factory function to generate the class for me:

def controllerFactory( controllerRequired, model ):
    if controllerRequired == "ViewController":
        return ViewController( model )
    elif controllerRequired == "FontController":
        return FontController( model )
    elif controllerRequired == "ColourController":
        return ColourController( model )
    else:
        raise TypeError

Is there a way to generate an instance of a class given the class name at
runtime? If so, what, in newbie terms, is it?




More information about the Python-list mailing list