Variable inside module

Marcelo A. Camelo camelo at
Mon Apr 14 22:08:51 CEST 2003


Can someone please explain-me what is 
happening here:

Given the followin 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

	from bar import *

Then, in the interactive python interpreter:

	>>> import foo
	>>> print foo.var
	>>> foo.PrintVar()
	var: foo
	>>> foo.SetVar()
	>>> foo.PrintVar()
	var: bar
	>>> print foo.var
What I don't understand is that foo.var seems 
to be two different variables: one accessed 
inside the file and one accessed from 
within the python interpreter. What is going on

Thank you,

Marcelo A. Camelo

More information about the Python-list mailing list