0

After reading at Python - Visibility of global variables in imported modules

I was curious about this example:

import shared_stuff
import module1

shared_stuff.a = 3
module1.f()

If there are no other variables "a" anywhere else, why the following one is not equivalent?

from shared_stuff import *
import module1

a = 3
module1.f()

We leave out "explicit is better than implicit": I am asking out of curiosity, as I prefer the first syntax anyway. I come from C and it looks like I didn't fully grasp Python's namespace's subtleties. Even a link to docs where this namespace's behaviour is explained is enough.

Community
  • 1
  • 1
Alex Poca
  • 2,125
  • 1
  • 23
  • 39

1 Answers1

1

Importing * copies all the references from the module into the current scope; there is no connection to the original module at all.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325