selecting a class in execution time
Brian Quinlan
brian at sweetapp.com
Mon Apr 7 13:23:51 EDT 2003
I'd be nice to know the why of this to suggest the best idiom.
> Object x = Class.forName( "mypackage.TheClass" ).newInstace();
x = getattr(__import__("mypackage"), "TheClass")()
The last set of parens is the argument list for TheClass' constructor.
If you know the name of the package at compile time then:
import mypackage
x = getattr(mypackage, "TheClass")()
Cheers,
Brian
More information about the Python-list
mailing list