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(): > print foo > > In an ipython session, I see the following: > > In [1]: from test import * > > In [2]: foo > Out[2]: [] > > In [3]: goo() > > In [4]: foo > Out[4]: [] > > In [5]: moo() > [2] > > I don't understand this behavior. I assumed that foo as defined in > test.py is a global object, but that doesn't seem to be the case.
``global`` means module global. There's no really global namespace in Python. > The ipython session seems to be dealing with one copy of foo while > goo() and moo() are using an entirely different copy. The import binds the list from the module to the name `foo` in the IPython namespace. When you call `goo()` it binds the name `foo` *within the module* to a new list. This has of course no influence on the name in the IPython namespace which is still bound to the list object that was bound to `test.foo` before. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list