Reference current module?

Steven Bethard steven.bethard at gmail.com
Tue Jun 19 21:34:50 CEST 2007


Tobiah wrote:
> Can I do:
> 
> getattr(current_module, 'foo')
> 
> where 'current_module' is a handle the the one
> that the code is in?  Just like
> 
> getattr(self, 'foo')
> 
> in a class, but for the current module instead?

You can try __import__() with __name__::

     >>> foo = 42
     >>> mod = __import__(__name__)
     >>> getattr(mod, 'foo')
     42

STeVe



More information about the Python-list mailing list