How to instatiate a class of which the name is only known at runtime?
Erik Max Francis
max at alcyone.com
Tue Sep 9 22:22:16 CEST 2003
Nick Welch wrote:
> exec("import %s" % modulename) ?
module = __import__(moduleName)
> class = getattr(modulename, classname)
> class = globals()[classname]
> and then:
> an_instance = class()
You'll need to use a name other than class, since that's a keyword.
Typically people use class_, klass, or cls.
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ But when I reached the finished line / Young black male
\__/ Ice Cube
More information about the Python-list