Why does interpreter flub

syed_saqib_ali at yahoo.com syed_saqib_ali at yahoo.com
Tue Mar 1 14:49:37 EST 2005









I have a file named testPython.py as shown below.

I have shown a trace of the Interpreter Session in which I import the
modules from this file using the command:
"from testPython import *"
When I do this, and modify a global variable from within a function, it
seems that the interpreter is unaware of the updated value!  See Trace
#1 to see what I'm talking about. I don't understand why. Could
somebody please supply a coherent explanation??

However, when I use the "import testPython" command instead, it seems
to work as I would expect. (See Trace #2). Why the difference??


==================START OF FILE==================
#!C:\python21\python.exe -u -d
x = 10
def main():
	global x
	print "In Main"
	x = 12

def printX():
	print x
if __name__ == "__main__":
	main()
================== END OF FILE ==================

==================START OF TRACE 1==================
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> from testPython import *
>>> x
10
>>> main()
In Main
>>> printX()
12
>>> x
10
>>>
================== END OF TRACE 1 ==================


==================START OF TRACE 2==================
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import testPython
>>> testPython.x
10
>>> testPython.main()
In Main
>>> testPython.printX()
12
>>> testPython.x
12
>>>
================== END OF TRACE 2 ==================




More information about the Python-list mailing list