[Python-Dev] LOAD_NAME & classes

Michael Gilfix mgilfix@eecs.tufts.edu
Tue, 23 Apr 2002 15:04:57 -0400

On Tue, Apr 23 @ 14:13, Aahz wrote:
> On Tue, Apr 23, 2002, Michael Gilfix wrote:
> >
> >   Has there ever been a discussion about some easy or straight-forward
> > way of sharing a global instance across modules? For example, in a
> > gui app, you might want to structure the program such that there's a
> > global instance app (main.app) of the application that other modules
> > might want to query. I was never to happy with importing main and then
> > using main.app... I felt like I wanted to qualify it as a global, like:
> > 
> >   from main import global app
> The simple way to do it, IMO, is to import a joint module, called
> something like cfg.  I just did a little hack in a script with
>     cfg = imp.new_module('cfg')
> but I don't know how well that works across modules.  I think you'd need
> to poke it into sys.modules in order for it to be shared, but I haven't
> tested it.

  Yeah, that's another good way to do it. Have some sort of
configuration object you could query. It would be nice if python
offered a facility for sharing pooled data: maybe a global
configuration object? That way it's more explicit. It could also
remove some redundant code that I tend to put in many applications.

  import config

  my_opt = config.get_opt ('mymodule')
  # Set a new global opt
  config.set_opt ('attrib') = blah

  Maybe even provide some hooks for populating the config object.

              -- Mike

Michael Gilfix

For my gpg public key: