How to instatiate a class of which the name is only known at runtime?

Gary Herron gherron at islandtraining.com
Tue Sep 9 15:56:25 EDT 2003


On Tuesday 09 September 2003 12:35 pm, Marco Herrn wrote:
> Hi,
>
> I am writing a program that has to instantiate a class from which I
> don't know the name until runtime. That leads to two problems for me.
>
> 1. How to do the import? I didn't find a way to give a string to the
>    import statement.
>
> 2. How to write such code to instantiate?

The imp module has what you want.  Here are two small protions of the manual:


find_module(name[, path])
  Try to find the module name on the search path path.
   ... much more ...


load_module(name, file, filename, description)
  Load a module that was previously found by find_module()
   ... much more ...

Enjoy,
Gary Herron







More information about the Python-list mailing list