How do can you make a variable static?

H. P. Friedrichs " HPeter.Friedrichs" at alliedsignal.com
Fri Jun 25 00:16:19 CEST 1999


Hello:
I hope somebody can help me with this problem (python newbie, <<week)

I have a dictionary called SymHanDict. It's entries are comprised of a
"symbol handle" number (the key), and a numeric value. A function foo()
resides in another module. When called, it's supposed to look at
SymHanDict, and create a new entry comprised of an incremented handle
number (the highest handle number in the existing dictionary plus one),
and a default data value of 0.

I keep getting name errors on SymHanDict, even though I've specified it
as global. I've seen the FAQ and thumbed through a borrowed copy of
Programming Python (M. Lutz). I don't see anything that allows me to
declare variables as static, and I don't understand the explanation of
globals in python.

Can anyone clarify? What's the workaround? For a variety of reasons I
won't go into right now, I really can't pass the dictionary as an
argument.

Thanks

Pete


**** the following contained in main module test.py
*****************************
from foo import foo

SymHanDict={0:0}

foo()                            #each call to foo() should add items to
the dictionary
foo()
foo()

*** the following contained in module foo.py ************************
def foo():
    global SymHanDict

    handlelist=SymHanDict.keys()          # get the list of existing
handles
    handlelist.sort()                     # sort the list, then

    handle=handlelist[len(handlelist)-1]        # find largest handle
    handle=handle+1                         # generate new handle, and
    SymHanDict[handle]=0                  # add it to dictionary along
with
                                          # a default value of zero.

    print SymHanDict




--

-----------------------------------------------
| The opinions expressed here are my own,     |
| and not necessarily those of my employer.   |
-----------------------------------------------






More information about the Python-list mailing list