Dynamically loading python classes

Brian Bull bbull at pathfire.com
Fri Apr 11 18:13:18 CEST 2003


I am writing an application that has to dynamically load classes from
other modules based on certain criteria.  I load the class initially
like this:

 self.dict["handleUnknown"] =
new.classobj("handleUnknown",(handleUnknown.handleUnknown,),{})

I later initialize this class by calling

runningClass = self.dict[clas](self.pluginLog)

where clas is the name of the class I want to load such as
"handleUnknown" in this example

I then execute methods against the class object and delete it when I
am finished.

What I am looking for is a way to load the class "fresh" each time I
instantiate it.  For instance, if I make a change to the loaded class,
I want that change to be activated the next time the class is loaded
without having to restart the main program that loads the classes.

I also need to be able to load the module and class from strings
instead of actually using the module/class name.  ie.

runningClass = handleUnknown.handleUnknown()

This is what I want to be able to do (something similar to this)
className = "handleUnknown"
runningClass = className.className()

Any help would be appreciated.  If you need a better explanation,
please email me (bbull at pathfire.com).  I can also send my code if
needed.




More information about the Python-list mailing list