Re: Problem with global variables

2008-08-09 Thread Nick Craig-Wood
Anthony Kuhlman [EMAIL PROTECTED] wrote: Pythoners, I'm having trouble understanding the behavior of global variables in a code I'm writing. I have a file, test.py, with the following contents foo = [] def goo(): global foo foo = [] foo.append(2) def moo():

Problem with global variables

2008-08-08 Thread Anthony Kuhlman
Pythoners, I'm having trouble understanding the behavior of global variables in a code I'm writing. I have a file, test.py, with the following contents foo = [] def goo(): global foo foo = [] foo.append(2) def moo(): print foo In an ipython session, I see the following: In

Re: Problem with global variables

2008-08-08 Thread Marc 'BlackJack' Rintsch
On Fri, 08 Aug 2008 13:10:48 -0400, Anthony Kuhlman wrote: I'm having trouble understanding the behavior of global variables in a code I'm writing. I have a file, test.py, with the following contents foo = [] def goo(): global foo foo = [] foo.append(2) def moo():

Re: problem with 'global'

2008-01-21 Thread Duncan Booth
Mel [EMAIL PROTECTED] wrote: oyster wrote: why the following 2 prg give different results? a.py is ok, but b.py is 'undefiend a' I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 #a.py def run(): if 1==2:# note, it

Re: problem with 'global'

2008-01-21 Thread Mel
Duncan Booth wrote: Mel [EMAIL PROTECTED] wrote: oyster wrote: why the following 2 prg give different results? a.py is ok, but b.py is 'undefiend a' I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 #a.py def run(): if 1==2:

Re: problem with 'global'

2008-01-21 Thread Gabriel Genellina
En Mon, 21 Jan 2008 11:44:54 -0200, Mel [EMAIL PROTECTED] escribi�: Duncan Booth wrote: The first sentence (which hasn't changed since 2.4) describing the global statement seems clear enough to me: The global statement is a declaration which holds for the entire current code block. I

Re: problem with 'global'

2008-01-21 Thread Marc 'BlackJack' Rintsch
On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote: The future statement is another example, even worse: if 0: from __future__ import with_statement with open(xxx) as f: print f In Python =2.5 it's a compile time error if that import is not the very first statement in

Re: problem with 'global'

2008-01-21 Thread Duncan Booth
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote: The future statement is another example, even worse: if 0: from __future__ import with_statement with open(xxx) as f: print f In Python =2.5 it's a compile time

Re: problem with 'global'

2008-01-21 Thread Gabriel Genellina
En Mon, 21 Jan 2008 17:36:29 -0200, Duncan Booth [EMAIL PROTECTED] escribi�: Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote: The future statement is another example, even worse: if 0: from __future__ import

problem with 'global'

2008-01-20 Thread oyster
why the following 2 prg give different results? a.py is ok, but b.py is 'undefiend a' I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 #a.py def run(): if 1==2:# note, it always False global a a=1 run() a

Re: problem with 'global'

2008-01-20 Thread Mel
oyster wrote: why the following 2 prg give different results? a.py is ok, but b.py is 'undefiend a' I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 #a.py def run(): if 1==2:# note, it always False global a

Re: problem with global var

2008-01-04 Thread Peter Otten
Bruno Ferreira wrote: I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Now that your immediate problem is solved it's time to look at the heapq module. It solves the problem of finding the N largest items in a list much more efficiently. I

Re: problem with global var

2008-01-04 Thread Bruno Ferreira
Hello all, Amazing :) The program is working properly now, the code is much better and I learned a bit more Python. Thank you all, guys. Bruno. 2008/1/4, Peter Otten [EMAIL PROTECTED]: Bruno Ferreira wrote: I wrote a very simple python program to generate a sorted list of lines from a

problem with global var

2008-01-03 Thread Bruno Ferreira
Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ## 1 logfile = open (squid_access.log, r) 2 topsquid = [[0, 0, 0, 0, 0, 0, 0]] 3 4 def add_sorted (list): 5 for i in

Re: problem with global var

2008-01-03 Thread Matt Nordhoff
Bruno Ferreira wrote: Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ## 1 logfile = open (squid_access.log, r) 2 topsquid = [[0, 0, 0, 0, 0, 0, 0]] 3 4 def

Re: problem with global var

2008-01-03 Thread wes
Bruno Ferreira wrote: Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ## 1 logfile = open (squid_access.log, r) 2 topsquid = [[0, 0, 0, 0, 0, 0, 0]] 3 4 def

Re: problem with global var

2008-01-03 Thread Diez B. Roggisch
Bruno Ferreira wrote: Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ## 1 logfile = open (squid_access.log, r) 2 topsquid = [[0, 0, 0, 0, 0, 0, 0]] 3 4 def

Re: problem with global var

2008-01-03 Thread Fredrik Lundh
Bruno Ferreira wrote: When I execute the program _without_ the lines 10 and 11: 10 if len(topsquid) 50: 11 topsquid = topsquid[0:50] it runs perfectly. But if I execute the program _with_ those lines, this exception is thrown: [EMAIL PROTECTED]:~$ python topsquid.py

Re: problem with global var

2008-01-03 Thread Steven D'Aprano
On Thu, 03 Jan 2008 11:38:48 -0300, Bruno Ferreira wrote: Hi, I wrote a very simple python program to generate a sorted list of lines from a squid access log file. Here is a simplified version: ## 1 logfile = open (squid_access.log, r) 2 topsquid =

Problem with global

2007-10-12 Thread Florian Lindner
Hello, I have a little problem with the global statement. def executeSQL(sql, *args): try: import pdb; pdb.set_trace() cursor = db.cursor() # db is type 'NoneType'. [...] except: print Problem contacting MySQL database. Please contact root

Re: Problem with global

2007-10-12 Thread Larry Bates
Florian Lindner wrote: Hello, I have a little problem with the global statement. def executeSQL(sql, *args): try: import pdb; pdb.set_trace() cursor = db.cursor() # db is type 'NoneType'. [...] except: print Problem contacting MySQL database

Re: Problem with global

2007-10-12 Thread Florian Lindner
Larry Bates wrote: Florian Lindner wrote: Hello, I have a little problem with the global statement. def executeSQL(sql, *args): try: import pdb; pdb.set_trace() cursor = db.cursor() # db is type 'NoneType'. [...] except: print Problem

Problem with global variables

2007-04-02 Thread Ed Jensen
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = [] for tmp in foo: bar.append(tmp) foo = bar if __name__ == __main__: foo = ['hello', 'world'] tiny() When I try to run

Re: Problem with global variables

2007-04-02 Thread hg
Ed Jensen wrote: #! /usr/bin/env python def tiny(): bar = [] for tmp in foo: bar.append(tmp) foo = bar if __name__ == __main__: foo = ['hello', 'world'] tiny() Like this ? #! /usr/bin/env python def tiny():     bar = [] gobal foo     for tmp in foo:         bar.append(tmp)    

Re: Problem with global variables

2007-04-02 Thread Laurent Pointal
Ed Jensen a écrit : I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = [] for tmp in foo: bar.append(tmp) foo = bar if __name__ == __main__: foo = ['hello', 'world

Re: Problem with global variables

2007-04-02 Thread Bjoern Schliessmann
Laurent Pointal wrote: And so the solution to add global foo before using it. Didn't you read his final question? | All of a sudden, tiny() can see the global variable foo. Very | confusing! Why is it that tiny() sometimes can, and sometimes | can't, see the global variable foo? I have no

Re: Problem with global variables

2007-04-02 Thread Steve Holden
Bjoern Schliessmann wrote: Laurent Pointal wrote: And so the solution to add global foo before using it. Didn't you read his final question? | All of a sudden, tiny() can see the global variable foo. Very | confusing! Why is it that tiny() sometimes can, and sometimes | can't, see

Re: Problem with global variables

2007-04-02 Thread Roel Schroeven
Bjoern Schliessmann schreef: Laurent Pointal wrote: And so the solution to add global foo before using it. Didn't you read his final question? | All of a sudden, tiny() can see the global variable foo. Very | confusing! Why is it that tiny() sometimes can, and sometimes | can't, see

Re: Problem with global variables

2007-04-02 Thread irstas
On Apr 2, 5:29 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Laurent Pointal wrote: And so the solution to add global foo before using it. Didn't you read his final question? | All of a sudden, tiny() can see the global variable foo. Very | confusing! Why is it that tiny()

Re: Problem with global variables

2007-04-02 Thread Ed Jensen
Ed Jensen [EMAIL PROTECTED] wrote: I'm having a vexing problem with global variables in Python. SNIP Thanks to everyone who replied. The peculiar way Python handles global variables in functions now makes sense to me. -- http://mail.python.org/mailman/listinfo/python-list

Re: Problem with global variables

2007-04-02 Thread Laurent Pointal
Bjoern Schliessmann wrote: Laurent Pointal wrote: And so the solution to add global foo before using it. Didn't you read his final question? Yes, and i replies: which contains a foo assignment. As foo is not defined global, it is considered to be local. Maybe my explanation was not

Re: Problem with global variables

2007-04-02 Thread Bjoern Schliessmann
Laurent Pointal wrote: Yes, and i replies: which contains a foo assignment. As foo is not defined global, it is considered to be local. Maybe my explanation was not clear enough with variable foo to be considered local because there is an *assignment* to foo. Yep, thanks for the

problem with global variable in a module

2006-11-25 Thread hollowspook
def aa(): global b b=b+1 print b b=1 aa() The above code runs well in python shell. However I have a problem as follows. new a file named test.py def aa(): global b b=b+1 print b

Re: problem with global variable in a module

2006-11-25 Thread Duncan Booth
hollowspook [EMAIL PROTECTED] wrote: from test import * b=1 aa() The error message is : Traceback (most recent call last): File interactive input, line 1, in ? File test.py, line 3, in aa b=b+1 NameError: global name 'b' is not defined Why this happens? Please do me a favor.

Re: problem with global variable in a module

2006-11-25 Thread Diez B. Roggisch
hollowspook schrieb: def aa(): global b b=b+1 print b b=1 aa() The above code runs well in python shell. However I have a problem as follows. new a file named test.py def aa(): global b

Re: problem with global variable in a module

2006-11-25 Thread hollowspook
Thanks for all the replys. Diez B. Roggisch 写道: hollowspook schrieb: def aa(): global b b=b+1 print b b=1 aa() The above code runs well in python shell. However I have a problem as follows. new a file named test.py