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():
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
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():
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
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:
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
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
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
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
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
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
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
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
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
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
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
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
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
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 =
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
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
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
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
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)
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
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
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
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
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()
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
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
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
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
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.
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
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
36 matches
Mail list logo