Style: global configuration data

Alex Martelli aleaxit at yahoo.com
Mon Nov 13 11:06:28 EST 2000


"Boudewijn Rempt" <boud at rempt.xs4all.nl> wrote in message
news:8uovrq$4ou$1 at news1.xs4all.nl...
    [snip]
> the warning in Internet Programming with Python (p. 173 - ... does not
> create a shared variable between the modules ...). But I see that they
> talk about what you say below:
>
> > PS:  You dont want to do
> > from config import setting
> > setting = 2
> > ... because it won't work.

Right, it won't: because 'setting' is an entry into the
dictionary of "this" module here.  Rebinding it to something
else does not affect any _other_ possible bindings to the
same object.


> Curiously enough, in the more complicated example below 'from config
> import Config' does work... Probably a silly oversight on my part,
> but I can't see the real difference:
> --------- config.py ----------
> class Config:
>   foo=10
    [snip]
> from config import Config
    [snip]
>     Config.foo="A"

Here, what's being re-bound is entry 'foo' in the dictionary of
(class) object Config.  If several modules are bound to that
same class object, they'll all see whatever the current bindings
*in ITS dictionary* are.


Maybe it would help get the right mental model if you saw

    from config import Config

as a useful shorthand for:

    locals()['Config'] = __import__('config').__dict__['Config']

Clearly (I hope), given that different modules (and functions)
see different directories returned by 'locals()', what they do
to the entries of those directories matter not to one another.
OK so far...?  (You can't usefully bind things to locals()'
entries inside a function, but, 'conceptually', that is what's
going on, more or less...).


Now, Config.foo="A" may similarly be seen as shorthand for:

    Config.__dict__['foo'] = "A"

Clearly, if different places all see the object that is here
denoted by 'Config', changes to its one-and-only dictionary
are also going to be equally seen by all those 'places'...


Alex






More information about the Python-list mailing list