What does the keyword 'global' really mean

Michael Chermside mcherm at mcherm.com
Tue Sep 9 08:05:56 EDT 2003


John Dean writes:
> I have looked [...] for a full explanation of what
> 'global' really means. Would I be
> correct in assuming that any variable prefixed with the keyword global would
> allow that variable to be accessible across translation units, in other
> words global is equivalent to the 'C' keyword 'extern' ?

It's a reasonable assumption, but no, that's not what global does in Python.

In Python, a "global" variable is accessible throughout a single module
(file), instead of throughout the entire program. It's normally just
those things (variables, functions and classes) that are defined at the
"top level" in the file. Of course, although these are only "global" in
the module, they can still be accessed from other modules via import.
For instance, "stdin" and "stdout" are two globals from the sys module,
and if you use "import sys", you can later read from "sys.stdin". But
if you were writing a function INSIDE the sys module, you could just use
"stdin" anywhere... inside a function, a method, anywhere. That's why
it's called "global".

There is, however, one exception: you are allowed to shadow a variable
by using another with the same name. Consider this code:

    # WARNING: Code not tested. Could have bugs.
    x = 7 # defines global variable

    def func_using_global():
        print 'x = %i' % x  # prints 7

    # notice that there are now two globals: "x" and "func_using_global"

    def outer_func():
        print 'start outer'
        def inner_func():
            print 'x = %i' % x  # ALSO prints 7
        inner_func()
        y = 8

    # now 3 globals, we added "outer_func". "y" and "inner_func" are
    #  NOT global.

    def func_using_x_for_other_purposes():
        x = 99
        print 'x has been defined as %i' % x  # prints 99

In the function "func_using_x_for_other_purposes", the name "x" is 
used for some other purpose, totally unrelated to the global variable
of the same name. That's ALLOWED, which is nice, because it means you
don't have to know about global variables you are writing a function
(except for those that you plan to use). But what if you wanted to
write a function that CHANGED the global x?

Well, that's what the "global" statement is for. Python assumes that
if you just USE a variable, without assigning to it, then it's the 
global variable of that name (if there is one!). But if you assign to
it anywhere in the function, then it assumes you have a local variable
that just happens to share the same name. If you really wanted to
CHANGE the global variable, then you add a "global" statement, like
this:

    def func_changing_global_x():
        global x
        x = x + 1  # changes the global "x"

> Also, does Python have the equivalent of the 'C' keyword 'static'?

If you really mean 'C''s "static" (not C++, which re-used it for
some wierd stuff), then the answer is "global"! Just create a global
variable, and choose to use it only within a particular function, 
and BOOM! there you go... a "static" variable. Any thread safety 
issues (if your program uses threads) are your own responsibility (same
as in C).

Hope this helps!

-- Michael Chermside








More information about the Python-list mailing list