By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,955 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,955 IT Pros & Developers. It's quick & easy.

Why does interpreter flub

P: n/a




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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
sy************@yahoo.com wrote:
"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!


Uh... don't do this then.

Beginners to Python should never use the "from xxx import *" form,
as this is just one of the types of problems it causes.

Instead, write only code like this, which is considered
proper Python style and won't have the same trouble:
from testPython import *
Use "import testPython" instead. If you don't like the
long name, you can use "import testPython as m" or
something like that. In the following, I'll assume
you took the second approach:
x
Use m.x instead....
main()
printX()
m.main()
m.printX()
x


And at this point use "m.x" again, and the answer
will be correct.

If you'd like to understand exactly why what you did doesn't
work, I strongly recommend you read the entire Python FAQ,
from start to finish. In the process, you'll get your answer,
and you'll learn an awful lot more that you need to know
(and that you didn't need to know) besides.

-Peter
Jul 18 '05 #2

P: n/a
sy************@yahoo.com 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/
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.