surprised by import in python 2.6

Stefaan Himpe stefaan.himpe at gmail.com
Fri Dec 10 17:06:43 EST 2010


Hello list,

Recently someone asked me this question, to which I could not give an 
answer. I'm hoping for some insight, or a manual page. What follows is 
python 2.6.

The problem is with the difference between

from test import *

and

import test

First things first. Here's the code to test.py:

a = 3
def f():
    global a
    return a

Now, in the interactive interpreter:

 >>> from test import *
 >>> a
3
 >>> f()
3
 >>> a = 4
 >>> f()
3

in a second session:

 >>> import test
 >>> test.a
3
 >>> test.f()
3
 >>> test.a = 4
 >>> test.f()
4

Somehow, in the first session I cannot modify the global variable a 
returned from f, but in the second session I can. To my eye, the only 
difference seems to be a namespace. Can anyone shine some light on this 
matter?

Thanks,
Stefaan.



More information about the Python-list mailing list