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)
>
> d
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 mo
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_
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.
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 sta
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
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():
>>
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:
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
> globa
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
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
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
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 topsq
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
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"
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"
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 .
>> [...]
>> except:
>> print "Problem con
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 .
> [...]
> except:
> print "Problem contacting MySQL database. Please c
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
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 w
Ed Jensen <[EMAIL PROTECTED]> wrote:
> I'm having a vexing problem with global variables in Python.
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
On Apr 2, 5:29 pm, 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, an
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
>
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
> |
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 hav
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 = ['
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.ap
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
> >
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():
"hollowspook" <[EMAIL PROTECTED]> wrote:
> from test import *
> b=1
> aa()
>
> The error message is :
> Traceback (most recent call last):
> File "", 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 favo
30 matches
Mail list logo