S
syed_saqib_ali
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.================== 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.================== END OF TRACE 2 ==================
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.================== 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.================== END OF TRACE 2 ==================