-   Programming (
-   -   Python ignores global statement? (

voyciz 04-07-2005 12:20 PM

Python ignores global statement?
Someone please help, I can't get the variables of any functions to become global! Maybe this is something really simple that I'm missing, I don't know, but take a look:


>>> def func():
...    global x
...    x = 99
>>> print x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined

Why? Shouldn't the "global" statement take care of this? What's going on? I need to do this for a program I'm working on, and can't get it to work. Any help would be greatly appreciated.

EDIT: I really feel like an idiot now. I NEVER RAN THE FUNCTION! Someone please delete this...

davholla 04-08-2005 08:25 AM

You are not calling the function anywhere you need to add this line :-

before you type print x.

Although it would be better to change the code to have the function return x
and then type :-

x - func()

voyciz 04-08-2005 12:03 PM

Thanks, davholla, but the edit in that post. ;)

davholla 04-08-2005 02:07 PM


Originally posted by Aborted Fetus
Thanks, davholla, but the edit in that post. ;)
Brilliant ! By mistakes we learn, my mistake was not reading the whole post !

All times are GMT -5. The time now is 12:23 PM.