Accessing one's main script's global from imported libraries

Alex Martelli aleax at aleax.it
Mon Feb 24 14:44:07 EST 2003


czrpb wrote:

> All:
> 
> Say I have spam.py with the global taste.
> Say in spam.py I import eggs.
> Say I want eggs to access spam.py's taste global.
> 
> How might I do this?

You shouldn't -- it sets up the worst possible kinds of
dependencies.  But if you're really dead set on it:

===spam.py:

taste = 23

import eggs


===eggs.py:

import __main__

print __main__.taste



and run "python spam.py".  This will allow you to write
a real disaster of an undebuggable, unmaintainable
multi-module program.


Alex





More information about the Python-list mailing list