Is global really global.

Steve Juranich sjuranic at condor.ee.washington.edu
Thu Oct 26 20:47:10 EDT 2000


So far, I really love Python.  However, the most confusing part of it for me
is definitely the whole namespace business.

I'm writing a suite of modules that are supposed to work in concert.  In the
"main" module, I read in a file that specifies all of the run-time paramters
that the program is supposed to use.  Here is where I define the "options"
structure:

    global options
    options = read_options(parfile)

Where the read_options function works a little magic on the file and returns
the table (one of my own classes) that contains all of the information that
the user specified in a parameter file (or "parfile").  This all happens in
a module called "Peaks.py".

Later on, I try and test some values in "options" via:

        if options.no_coda:
            # Eventually, here's where I'd remove the coda, but for
            # now, just...
            raise NotImplementedError, \
                  "I can't deal with the no_coda option yet."        

Please ignore the fact that I don't have the option implemented yet. <0.5 wink> 
The test above happens in a module called "Locator.py".  But the above test
when I try to run it, gives me:

condor (peaks)$ Peaks.py parfile.peaks first_list First
Traceback (most recent call last):
  File "./Peaks.py", line 75, in ?
    main_loop()
  File "./Peaks.py", line 66, in main_loop
    peaks = Locator.f0_peak_locator(frames)
  File "./Locator.py", line 23, in f0_peak_locator
    if options.no_coda:
NameError: options

Why is this happenning even though I specified "options" as a global?  This
causes me to suspect that "global" means something different in Python than
it does in C (which isn't a bad idea).

Am I misunderstanding the meaning of "global", or is something else afoot?
How do I work around this?  I know using globals isn't good anyway, but I'm
to the point that I _really_ need to show my professor that I've been doing
more than sitting on my thumb for the past couple of weeks (read: I need to
get something done _quickly_).

Thanks for the help.

trying-to-adjust-to-life-in-grad-school-ly y'rs,

----------------------------------------------------------------------
Stephen W. Juranich                         sjuranic at ee.washington.edu
Electrical Engineering         http://students.washington.edu/sjuranic
University of Washington             http://rcs.ee.washington.edu/ssli





More information about the Python-list mailing list