dynamic class instantiation

Nick Collier nick at src.uchicago.edu
Sat Sep 9 00:39:27 CEST 2000

I've written some code that will take a class name and create an
instance of the class, but was wondering if this is an appopriate way to
do it. I use imp.find_module and imp.load_module to get the module that
the class resides in. Then I do:

class_name = 'SomeClass'
clazz = module.__dict__[class_name]
instance = clazz()   // no arg constructor

This seems to work okay, but I'm wondering if there is some canonical
way of creating an instance of a class from only its fully qualified
name (i.e. 'package.module.Class').



Nick Collier
Social Science Research Computing
University of Chicago

More information about the Python-list mailing list