surprised by import in python 2.6

Ian ian.g.kelly at gmail.com
Fri Dec 10 17:22:54 EST 2010


On Dec 10, 3:06 pm, Stefaan Himpe <stefaan.hi... at gmail.com> wrote:
> 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?

It's not the same global variable.  In the second session, you import
the module test and bind it to the name "test" in the main namespace.
"test.a" and "test.f" refer to the objects named "a" and "f" in the
test namespace.

In the first session, you import all the variables exported by the
module test and bind them using the same names in the main namespace.
Thus "a" and "test.a" refer to the same int; and "f" and "test.f"
refer to the same function, but they are not the same variables.  When
you rebind the name "a", it does not also magically rebind "test.a",
and vice versa.

Cheers,
Ian



More information about the Python-list mailing list