How to pass a reference to the current module

James Stroud jstroud at
Sat Aug 4 02:22:40 CEST 2007

Hello All,

Say I have this code:

import AModule
import ModuleUser

ModuleUser.do_something_with(AModule, 'some_function_name')

But say the functions are defined in the same module that contains the 
above code. I can imagine this hack:

import AModule
import ModuleUser

ModuleUser.do_something_with(AModule.__name__, 'some_name')

Where AModule.__name__ could be substituted for __name__ under the 
proper circumstances. Then, I would have this code in ModuleUser:

import sys
def do_something_with(modname, funcname):
    afunction = sys.modules[modname].funcname

Which is terribly ugly to me and makes for a pretty miserable API in my 
opinion, requiring the programmer to type underscores.

Basically, what I am trying to acomplish is to be able to do this in any 
arbitrary module or __main__:

funcname = determined_externally()
ModuleUser.do_something_with(AModule, funcname)

Ideally, it would be nice to leave out AModule if the functions were 
designed in the same namespace in which do_something_with is called.

Thanks in advance for any suggestions.


James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

More information about the Python-list mailing list