How to pass a reference to the current module

James Stroud jstroud at mbi.ucla.edu
Fri Aug 3 20:22:40 EDT 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
    [etc.]


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


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

http://www.jamesstroud.com/



More information about the Python-list mailing list