Instantiate arbitrary classes at run time

jiml at longson.com jiml at longson.com
Thu Mar 30 10:06:47 EST 2000


I am trying to create an instance of an arbitrary class at run time.
I can load the class easily enough by
target = 'arbitrary_class_name'
command = "from " + str(target) + " import " + str(target)
exec( command )
But can't figure out how to create an instance of the class without
using the class name
instance = arbitrary_class_name()
works fine, but I need to be able to create that instance based on the
string stored in target
In Java, I would code something like
instance = Class.forName( target );
and that would produce the same result as
instace = new arbitrary_class_name( )
I suspect the same capability exists in Python, but I haven't figured
out how to find it in the documentation.


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list