How to pass a reference to the current module

James Stroud jstroud at mbi.ucla.edu
Fri Aug 3 20:53:23 EDT 2007


James Stroud wrote:
> 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.


Is this the preferred way?


import sys
def do_something_with(funcname, amodule=None):
   if amodule is None:
     function = sys._getframe(1).f_locals[funcname]
   else:
     function = getattr(amodule, funcname)
   [etc.]


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