A startup puzzle

Glenn Andreas gandreas at no.reply
Mon Sep 29 15:16:12 EDT 2003


In article <vngnif6v8ip4c6 at corp.supernews.com>,
 "Edward K. Ream" <edreamleo at charter.net> wrote:

> I've just about convinced myself there is no good, clean solution to the
> following puzzle.  I wonder if you agree.
[snip]
> 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.

Maybe I'm missing something, but it seems like you want some sort of 
lazy initialization thing here.  Perhaps using a proxy object sort of 
like this might work:


realApp = None
class LeoProxy:
   def __getattr__(self,attr):
      if not realApp:
         import leoApp
         realApp = leoApp.leoApp()
      return getattr(realApp, attr)

app = LeoProxy()


Then you can import that module as often as you want, but the first time 
you do something with leoApp.app (i.e., "leoApp.app.doSomething()"), it 
will only then call the "real" function to create it (and then proxy it 
over as needed)




More information about the Python-list mailing list