Make variable global

Steve Holden steve at holdenweb.com
Wed Mar 21 16:43:28 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?
> 
> thanks
> 
That "blah" is global to module a, not to the whole program. You import 
all names from a, which creates a blah and a go in your local namespace, 
bound to the sames values those names have in module a.

When you call go() this sets a.blah to 5, but __main__.blah is still None.

It's those dratted namespaces again!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list