Variable inside module
Marcelo A. Camelo
camelo at esss.com.br
Mon Apr 14 16:08:51 EDT 2003
Hi!
Can someone please explain-me what is
happening here:
Given the followin bar.py file (placed
inside a foo/ directory):
var = 'foo'
def SetVar():
global var
var = 'bar'
def PrintVar():
global var
print 'var:', var
In the same foo/ directory, I've placed
the following __init__.py:
from bar import *
Then, in the interactive python interpreter:
>>> import foo
>>> print foo.var
foo
>>> foo.PrintVar()
var: foo
>>> foo.SetVar()
>>> foo.PrintVar()
var: bar
>>> print foo.var
foo
>>>
What I don't understand is that foo.var seems
to be two different variables: one accessed
inside the bar.py file and one accessed from
within the python interpreter. What is going on
here?
Thank you,
Marcelo A. Camelo
More information about the Python-list
mailing list