[Tutor] Global presets ?
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Dec 4 18:34:35 CET 2004
Kent Johnson said unto the world upon 2004-12-04 06:21:
> You are on the right track. Put your common definitions in a
> configuration module like this:
> # Config.py
> Then in client code, import Config. When you use the names defined in
> Config you have to prefix them with the module name like this:
> Alternately you can use either of these forms:
> or this:
> # Get *all* names defined in Config into our global namespace
> from Config import *
> to make the bare names available in the client.
> Dave S wrote:
>> Hi there,
>> I have some common data directories, like
>> which increasing numbers of scripts are accessing. At the begining of
>> each script
>> I end up putting in declarations like
>> over & over. This is OK until I want to move a directory
>> Somewhere I read about importing a script to define common globals for
>> all the scripts that import it.
>> I tried this, and failed - the variable was only valid for the module,
>> to be expected really :)
>> Can anyone make a suggestion howto set up common global presets.
Hi Dave, Kent, and all,
I have a caution about the
from Config import *
idiom that Kent didn't mention.
It can lead to namespace pollution, in that if you have a module 'foo'
with a name 'bar' and you are witting a script which says
from foo import *
you have to be very careful that your script doesn't also assign to the
name 'bar', else you may end up thinking you have two different things
available when you don't. ('bar' will either point to your script's bar
or to Config.bar, depending on whether you imported Config before or
after your scripts assignment to bar.)
The first time this bites you, it can eat up hours of your life. (But
I'm not bitter;-)
I avoid this by using the
import examplemodule as em
That imports everything so that you accesses it by
I find that really handy for the handful of utility modules I import
into most of my scripts. Then, I just have to be sure to avoid a small
set of names -- 'em' in this case. And my python files have nice
descriptive names, but I only have to type then once.
More information about the Tutor