breaking up is hard to do

Kent Johnson kent37 at tds.net
Fri Mar 25 17:36:56 EST 2005


bbands wrote:
> For example I have a class named Indicators. If I cut it out and put it
> in a file call Ind.py then "from Ind import Indicators" the class can
> no longer see my globals. This is true even when the import occurs
> after the config file has been read and parsed.

globals aren't all that global, they have module scope. One solution is to have a module just for 
your config data. Other modules that need to access it import the module and use its data. The 
config module can read it's values from a file or whatever when it is loaded.

Very simple example:

# Config.py

maxSize = 1000
# read values from config file and put them in module scope
# etc...


# Indicators.py

import Config

if xx > Config.maxSize:
   ...

If you really don't like to prefix all the references to Config with the module name you can use
   from Config import *
but that is not recommended...

Another solution is to put your config data into some kind of object or structure that you pass 
around to anyone who needs it.

Kent



More information about the Python-list mailing list