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

Hans Nowak hnowak@cuci.nl
Thu, 23 Aug 2001 11:11:04 +0200


>===== Original Message From "Hanna Joo" <hanna@chagford.com> =====
>Hi
>
>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.

Well, you can use getattr:

def something(modulename, funcname):
    mod = __import__(modulename)
    return getattr(mod, funcname)

HTH,

--Hans Nowak