Passing Global Variables between imported modules

Sean sean at
Mon Aug 4 16:27:41 CEST 2003

Is there any way to access global variables defined from within an
imported module?  For example, lets say have a file called
that defines a simple class:

class MyClass:
  def __init__(self):
  def printGlobal(self):
    print globalVar

Now I have another file that imports test2, sets a global variable
called globalVar, and calles printGlobal() like so:

from test2 import MyClass

globalVar = "foo"

mc = MyClass()

When I run it, I get a NameError that "global name 'globalVar' is not
defined".  I've tried explicitly calling 'global globalVar' and even
stepping through the __builtins__ but to no avail.  Is accessing
globalVar from within possible?

-Sean Levatino

More information about the Python-list mailing list