A startup puzzle

Edward K. Ream edreamleo at charter.net
Mon Sep 29 18:31:05 CEST 2003

I've just about convinced myself there is no good, clean solution to the
following puzzle.  I wonder if you agree.

1. My app has a module called leoGlobals that contains often-used global
functions.  All of Leo's source files start with:

from leoGlobals import *

I don't want to discuss whether this is good style: it is simple and it
works well for me :-)

2.  All code accesses the singleton application object using the app()
method in leoGlobals.

3.  To make the code cleaner looking, I would really like the code to be
able to access an app global instead of the app() function.  This has
nothing to do with speed: it's simply that there are lots of references to
app() in the code, and there is soon going to be a lot more references to
app.gui.x() and app.gui.y() etc.

As a workaround, many methods and functions assign a = app(), but I would
like to avoid this step.

4. Alas, it does not seem possible to initialize an app global in
leoGlobals.  The reason is simple:

from leoGlobals import *

caches the value of app at the time the  import is done.  But the startup
code that creates the app "global" (really an attribute of the leoGlobals
module) must do _other_ imports.

For example, doing the following at the top of leoGlobals doesn't work:

import leoApp
app = leoApp.leoApp() # construct the app instance.

Indeed, the leoApp module will be imported before the assignment of app.
Moreover, the leoApp module does other imports, and all the app variables in
those modules will be uninitialized.

It would be horrible style to place detailed constraints on the order in
which modules get imported, and I'm not sure even that would work.

Any ideas for a clean solution?  Thanks.


P.S.  The solution must work in 2.1, 2.2 and 2.3.

Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html

More information about the Python-list mailing list