A startup puzzle

Robert Brewer fumanchu at amor.org
Mon Sep 29 18:45:55 CEST 2003

Consider placing initialization code in the __init__.py file for your package?
Robert Brewer

	-----Original Message----- 
	From: Edward K. Ream [mailto:edreamleo at charter.net] 
	Sent: Mon 9/29/2003 9:31 AM 
	To: python-list at python.org 
	Subject: A startup puzzle

	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