Passing Global Variables between imported modules

StasZ stasz at tux.org
Mon Aug 4 20:24:29 CEST 2003


On Mon, 04 Aug 2003 07:27:41 -0700, Sean wrote:

> Is there any way to access global variables defined from within an
> imported module?  For example, lets say have a file called test2.py
> that defines a simple class:
> 
> class MyClass:
>   def __init__(self):
>     pass
>     
>   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()
> mc.printGlobal()
> 
You can try overloading MyClass.

from test2 import MyClass

globalVar = "foo"

class HisClass(MyClass):

    def printGlobal(self):
        print globalVar
 

mc = HisClass()
mc.printGlobal()

Stas Z





More information about the Python-list mailing list