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) > > d

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 mo

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 [1]

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

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 w

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. 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 irstas
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

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 >

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 > |

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 hav

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 >

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.ap

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', 'wor