Style: global configuration data

Michael Hudson mwh21 at cam.ac.uk
Mon Nov 13 09:00:26 EST 2000


boud at rempt.xs4all.nl (Boudewijn Rempt) writes:

> Michael Hudson <mwh21 at cam.ac.uk> wrote:
> > boud at rempt.xs4all.nl (Boudewijn Rempt) writes:
> 
> >> I have been wondering for some time about the best way to offer
> >> configuration data and other global resources to all modules and classes
> >> in my application, other than creating a config object and passing that
> >> on with every constructor.  What's the received Pythonic wisdom on this?
> >> 
> >> I'd prefer to just have one instance of my config class, or my repository
> >> class or whatever, so if I were working in Java I'd create a singleton, but
> >> that somehow doesn't seem right for Python to me.
> 
> > Having a module "config"?  Modules are handy in the way that if two
> > modules both go
> 
> > import config
> 
> > they get the same module.
> 
> > If config.py you could read the config from a ~/.rc file, the
> > registry, star patterns, whatever.  I can think of situations where
> > this might not work, but it has the benfit of being simple...
> 
> 
> I wonder - does that also work when one of the client modules changes
> a setting in the config? Are the other modules then updated?

Yes, if I understand the question.

E.g

--- start config.py
setting = 1
--- start a.py
import config

def f():
    return config.setting
--- start b.py
import config

def f(): config.setting = 2
--- start main.py
import a,b
print a.f()
b.f()
print a.f()

then 

$ python main.py
1
2

In the conceptual model I have of Python, there's no way anything else
could happen ... what were you expecting?

Cheers,
M.

PS:  You dont want to do
from config import setting
setting = 2
... becuase it won't work.

-- 
  I would hereby duly point you at the website for the current pedal
  powered submarine world underwater speed record, except I've lost
  the URL.                                         -- Callas, cam.misc



More information about the Python-list mailing list