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

marshall marshall at spamhole.com
Sun Mar 2 00:00:57 EST 2003


John La Rooy <nospampls.jlr at doctor.com> wrote in message news:<3e5e9205$0$13156$5a62ac22 at freenews.iinet.net.au>...
> marshall wrote:
> > I think the more general question is: "What is the best way to split a
> > large program up into several modules and still let them all have
> > access to application global data?"
> > 
> > What I have been doing is creating an Application instance which has
> > all of the global data (like maxscreensize, datapath, etc.) as
> > attributes.  Then I pass this App instance as a parameter to every
> > other object that the application creates.  If there is a better way I
> > would sure like to hear about it.
> 
> Would something like this work for you?
> 
> 
> ===globaldemo.py===
> #!/usr/local/bin/python
> import mod1
> import mod2
> mod1.setx()
> mod2.printx()
> 
> ===mod1.py===
> import globals
> def setx():
>      globals.x=10
> 
> ===mod2.py===
> import globals
> def printx():
>      print globals.x
> 
> ===globals.py===
> # nothing needs to be in here
> 
> John

Thanks John, I didn't know I could do that. 

I guess I've been struggling with something that really isn't all that
important - splitting source files up for ease of reading and
maintenance.  (Which is not, BTW, the OP's question).  Looking through
example source I see that I have been source files up too much - like
one per class.

Marshall




More information about the Python-list mailing list