[EMAIL PROTECTED] wrote:







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??

The interpreter hasn't flubbed, you have :-)

==================START OF FILE================== #!C:\python21\python.exe -u -d x = 10

This establishes a binding of the value 10 tot he name x within module testPython.


def main():
        global x
        print "In Main"
        x = 12

This prints a string and then rebinds the name testPython.x to the value 12.

def printX():
        print x

This prints the current value of testPython.x.

if __name__ == "__main__":
        main()
================== END OF FILE ==================

Overall, therefore, when imported or executed, the module binds the *module-global* name x to the value 10, the name main to one function and the name printX to a second. If it's being executed the module then calls the main() function.

==================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 *

This statement imports all names, with their current bindings, from the testPython module into the module running in the interactive interpreter (which is by definition "__main__").


x

10

main()

This call to main rebinds testPython.x to the value 12.

In Main

printX()

This call to printX prints the current value of testPython.x

12

x

This prints the (repr of) __main__.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 ==================

In your second example you are explicitly referring to the x from the namespace of the testPython module. In your first you are referring to the x from the __main__ module namespace.

from module import name

does not cause two names to be associated wight the same binding. It copies the current binding from one namespace into the importing namespace. The two bindings are thereafter independent, so changing the binding of testPython.x makes no difference to __main__.x.

regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to