Is global really global.

Quinn Dunkan quinn at pfennig.ugcs.caltech.edu
Fri Oct 27 00:06:50 EDT 2000


On Thu, 26 Oct 2000 17:47:10 -0700, Steve Juranich
<sjuranic at condor.ee.washington.edu> wrote:
>So far, I really love Python.  However, the most confusing part of it for me
>is definitely the whole namespace business.

No, python's 'global' is not really global :)

It's 'module global' (that's a feature).

Python's namespaces are really simple: there's local, and module global, and
that's all (well, except builtins).

>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)

Also note that any name bound at the module top-level will automatically be
module global, in that case the global declaration has no effect.  The sole
purpose of 'global' as far as I can tell is to be able to rebind globals from
within a local namespace, e.g.:

x = 5
def set_x(y):
    global x
    x = y # without the 'global' this would create a local 'x' that shadows
    # the global one

Not that you ever need to do that, right? :)  Anyway, to read module globals,
just import the module qualified and address them with dot notation:

# this is module Peaks.py
options = read_options(parfile)

# this is module Locator.py
import Peaks

if Peaks.options.no_code:
    blah blah blah


And in my opinion using globals is pretty kosher python as long as you don't
go and mutate them, but I'm no expert :)



More information about the Python-list mailing list