Make variable global

Gary Herron gherron at islandtraining.com
Wed Mar 21 16:34:26 EDT 2007


abcd wrote:
> I have a file, "a.py"
>
> blah = None
> def go():
>     global blah
>     blah = 5
>
> >From the python interpreter I try....
>
>   
>>>> from a import *
>>>> blah
>>>> go()
>>>> blah
>>>>
>>>>         
>
> ...i was hoping to see "5" get printed out the second time I displayed
> blah, but it doesn't.  Now, if I type this same code directly into the
> python interpreter it works as i was hoping.  what i am missing?
>   
Since procedure go is defined in a.py, the global blah it refers to
is global to that module.

So import a (instead of importing * from a) and try this:
 >>> import a
 >>> a.blah
 >>> a.go()
 >>> a.blah
5
 >>>


Gary Herron




More information about the Python-list mailing list