How do can you make a variable static?

Greg Ewing greg.ewing at compaq.com
Mon Jun 28 02:07:38 CEST 1999


The reason you're getting a NameError is that
the name SymHandDict is in a different module
from the one where you're referring to it.
The "global" statement you put in doesn't make
any difference to that.

For what you're doing here, you don't need or
want a "global" statement at all. You only need
one if you want to assign to a module-level variable
from within a function defined in the same module.
You're not doing that here. You're changing the
contents of the object to which SymHandDict refers,
but you're not assigning to the variable SymHandDict 
itself -- it still refers to the same object afterwards.

In general, the way you refer to a name in a *different*
module is by prefixing it with the name of the module
(which you must also import using an "import" statement),
e.g. in module foo:

import test

  def foo():
      handlelist=test.SymHanDict.keys() 
      ...
      test.SymHanDict[handle]=0

***BUT*** I wouldn't recommend you do it exactly that
way in this case, because then your two modules would
be importing from each other, a situation which can
lead to difficulties and is best avoided if possible.

If there will only ever be one instance of SymHanDict,
I'd suggest moving it either into module foo or some
other module which foo imports. 

If you put it in module foo, you will be able to refer
to it directly without any prefix:

  # in module foo:

  SymHanDict = {0:0}

  def foo():
      handlelist = SymHanDict.keys()
      ...
      SymHanDict[handle] = 0

If module test needs to decide how to initialise
SymHanDict, it can do this:

  # in module test:
  import foo
  foo.SymHanDict = {0:0}

> I don't understand the explanation of
> globals in python.

Keep in mind that there are only two namespaces you
can reference without using any prefix: (1) the
"local" scope (parameters and local variables of the
current function) and (2) the so-called "global"
scope, which is not completely global, but is
only the top level of the current module (i.e.
the one containing the piece of code being
executed).

To refer to anything in another module, you have
to use the module.name syntax.

Finally, the "global" statement is only needed in
one special circumstance, which is quite rare:
when you want to assign to a name in the current
module from within a function, without prefixing it 
with a module name. For example,

  x = 42

  def blarg():
    global x
    x = 17

  blarg()
  print x

prints 17. If the "global" statement weren't there,
the assignment would assign to a *local* variable
called x, and the global x would be unchanged.

However, note that you *don't* need a global
statement to do this:

   x = 42

   def blarg():
     print x

nor do you need one to do this:

   import banana

   def blarg():
     banana.x = 17

   blarg()
   print banana.x

Hope I've helped more than I've confused,
Greg

   




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