467,168 Members | 990 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

howto get function from module, known by string names?

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
  • viewed: 4711
Share:
7 Replies
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
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
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
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
Sorry, some problems with internet connection yield these messages

May 21 '07 #6
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
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.

Similar topics

9 posts views Thread by Penn Markham | last post: by
2 posts views Thread by Marc Shapiro | last post: by
4 posts views Thread by Andy | last post: by
7 posts views Thread by Guy Robinson | last post: by
5 posts views Thread by Bruce Lawrence | last post: by
8 posts views Thread by Gillard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.