Can module access global from __main__?

Steve Holden steve at holdenweb.com
Tue Oct 11 11:18:58 EDT 2005


Neal Becker wrote:
> Everything you said is absolutely correct.  I was being lazy.  I had a main
> program in module, and wanted to reorganize it, putting most of it into a
> new module.  Being python, it actually only took a small effort to fix this
> properly, so that in B.py, what were global variables are now passed as
> arguments to class constructors and functions.
> 
> Still curious about the answer.  If I know that I am imported from __main__,
> then I can do access X as sys.modules[__main__].X.  In general, I don't
> know how to determine who is importing me.
> 
I don't think you can without huge amounts of introspection - it's even 
worse than the "what's the name of this object" question that seems to 
come up regularly. A module can be imported from multiple modules, and 
you only get to execute code on the first import. Even then (on the 
first import) I am not sure how you could introspect to find the answer 
you want.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list