LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python ignores global statement? (http://www.linuxquestions.org/questions/programming-9/python-ignores-global-statement-310780/)

voyciz 04-07-2005 01: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:

Code:

>>> 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 09:25 AM

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

func()
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 01:03 PM

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

davholla 04-08-2005 03:07 PM

Quote:

Originally posted by Aborted Fetus
Thanks, davholla, but uhh...read 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 07:57 AM.