Why does interpreter flub

Peter Hansen peter at engcorp.com
Tue Mar 1 15:19:48 EST 2005


syed_saqib_ali at 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



More information about the Python-list mailing list