howto get function from module, known by string names?

dmitrey openopt at ukr.net
Tue May 15 07:29:56 EDT 2007


hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like

def myfunc(module_string1, func_string2, *args):
    eval('from ' + module_string1  + 'import ' + func_string2')
    return func_string2(*args)

or, if it's impossible, suppose I have some modules
module1 module2 module3 ... etc
each module has it own funcs 'alfa', 'beta' and class module1,
module2,... with same string name as module name.
can I somehow pass module as function param and then import function
'alfa' from (for example) module8? and/or import class moduleN from
moduleN?

something like
def myfunc(moduleK, *args):
    return moduleK.moduleK(*args)
or return moduleK.alfa(*args)

Thx, D




More information about the Python-list mailing list