By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

howto get function from module, known by string names?

P: n/a
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

May 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 15 May 2007 04:29:56 -0700, dmitrey wrote
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)
To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.

Together, that becomes something like this:
>>def myfunc(module_string1, func_string2, *args):
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
....
>>myfunc("math", "sin", 0)
0.0
>>myfunc("operator", "add", 2, 3)
5

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net

May 15 '07 #2

P: n/a
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.

Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
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)

To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.

Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
May 21 '07 #3

P: n/a
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.

Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
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)

To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.

Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
May 21 '07 #4

P: n/a
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.

Carsten Haese wrote:
On 15 May 2007 04:29:56 -0700, dmitrey wrote
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)

To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.

Together, that becomes something like this:
>def myfunc(module_string1, func_string2, *args):
... func = getattr(__import__(module_string1), func_string2)
... return func(*args)
...
>myfunc("math", "sin", 0)
0.0
>myfunc("operator", "add", 2, 3)
5

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
May 21 '07 #5

P: n/a
Sorry, some problems with internet connection yield these messages

May 21 '07 #6

P: n/a
En Mon, 21 May 2007 10:13:02 -0300, dmitrey <op*****@ukr.netescribió:
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
What's wrong with this:

try: import asdf
except ImportError: asdf = None

....later...

if asdf:
asdf.zxcv(1)
...

--
Gabriel Genellina

May 21 '07 #7

P: n/a
On Mon, 2007-05-21 at 06:13 -0700, dmitrey wrote:
And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Why would you need to do that? What are you planning to do when you have
determined that the module doesn't exist? Surely you're not planning on
swallowing the error silently, because that would make your program very
hard to debug. The Pythonic response is to raise an exception, but then
you might as well just propagate the exception that __import__ already
raises:
>>def myfunc(module_string1, func_string2, *args):
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
....
>>myfunc("asdf", "spam")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in myfunc
ImportError: No module named asdf

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net
May 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.