[Tutor] How to import one function from another program (not as header)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 23 Aug 2001 21:10:11 -0700 (PDT)


On Thu, 23 Aug 2001, Hanna Joo wrote:

> I have two programs. I would like to import a function from another program
> but want to pass name of the function as a variable. Is this possible?
> 
> prog a:
> 
> def something(modulename, funcName):
>     module = __import__(modulename)
>     func = module.funcName    ( == doesn't work.. complains that module has
> no attribute 'funcName')
> 
> I would like to keep funcName as a variable.. How can I do this? Any
> help will be appreciated.

Remco mentioned that getattr() is a good way to grab the function out of a
module, given that all we have is its name in a string.

Out of curiosity, what sort of program are you writing?  Perhaps there
might be an easier way to do what you're trying to do.

Talk to you later!