Instantiate arbitrary classes at run time

Anthony J Wilkinson anthony at dstc.edu.au
Thu Mar 30 10:35:08 EST 2000


On Thu, 30 Mar 2000 jiml at longson.com wrote:

> 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

Why not use the same method as above:

command = 'instance = ' + target + '()'
exec(command)


Regards,
Anthony





More information about the Python-list mailing list