Factory function to generate a named class
Peter Otten
__peter__ at web.de
Tue Sep 23 07:47:49 EDT 2003
Derek Fountain wrote:
> 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?
class FontController:
def __init__(self, model):
pass
class Model:
pass
def getClass(classname, modulename="__main__"):
return getattr(__import__(modulename, globals(), locals(), [classname]),
classname)
model = getClass("Model")()
print getClass("FontController")(model)
Should be fairly self-explanatory :-)
Peter
More information about the Python-list
mailing list