Global var access in imported modules?

Fredrik Lundh fredrik at pythonware.com
Wed Aug 27 17:08:26 EDT 2008


RgeeK wrote:

> I have a main module doStuff.py and another module utility.py.  At the 
> start of doStuff.py I call
> 
>    import utility.py

that tries to import a module named "py" from the package "utility".

> Then I also proceed to initiallize some global variables
> 
> sName = ""

> Within my utility.py file, I define the makeOne function. But I want to 
> use that same global variable "sName"  In utility.py I have tried to 
> indicate that I'm using the global "sName" through the statement:
> 
>   global sName

the "global" directive in Python is used *inside* a function or method 
to indicate that a given name is not local.

Python doesn't have "program-wide global" variables; if you need that, 
create a support module and import that module everywhere you need to 
access those variables:

     # file: globalvars.py
     sName = ""

     # file: myprogram.py
     import globalvars
     print globalvars.sName

etc.

</F>




More information about the Python-list mailing list