The inspect
module might help you. Here's some code from a plugin manager class I wrote that might serve as an example.
def load_plugin_file(self, pathname):
'''Return plugin classes in a plugin file.'''
name, ext = os.path.splitext(os.path.basename(pathname))
f = file(pathname, 'r')
module = imp.load_module(name, f, pathname,
('.py', 'r', imp.PY_SOURCE))
f.close()
plugins = []
for dummy, member in inspect.getmembers(module, inspect.isclass):
if issubclass(member, Plugin):
p = member(*self.plugin_arguments,
**self.plugin_keyword_arguments)
if self.compatible_version(p.required_application_version):
plugins.append(p)
return plugins
The other way to do it might be to use the builtin functions globals
, issubclass
, and isinstance
.