Why does interpreter flub

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

Peter Hansen

"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:

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:

Use m.x instead....
m.main()
m.printX()

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
 
S

Steve Holden

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.

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__").
This call to main rebinds testPython.x to the value 12.
This call to printX prints the current value of testPython.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.

In Main

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top