LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-07-2005, 12:20 PM   #1
voyciz
Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 425

Rep: Reputation: 40
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...

Last edited by voyciz; 04-07-2005 at 12:32 PM.
 
Old 04-08-2005, 08:25 AM   #2
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 652

Rep: Reputation: 31
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()
 
Old 04-08-2005, 12:03 PM   #3
voyciz
Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 425

Original Poster
Rep: Reputation: 40
Thanks, davholla, but uhh...read the edit in that post.
 
Old 04-08-2005, 02:07 PM   #4
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 652

Rep: Reputation: 31
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 !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Global Variable? ElementNine Programming 5 11-05-2007 12:53 PM
iptables ignores rules bCookie Linux - Networking 1 01-27-2005 02:49 AM
python global variable? goestin Programming 1 01-20-2005 06:52 AM
Shorewall ignores the rules? N3K0KUN Linux - Security 3 09-06-2004 02:48 PM
Python EXEC Statement ranger_nemo Programming 3 07-24-2003 08:31 PM


All times are GMT -5. The time now is 05:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration