title: Value of variable referenced from another module does not change after it is changed in its own module
body: <p>I started to write python programs for some time, but I am not so good at it, at present I meet a problem, it seems very strange, at least for me.</p>
  
  <p>Assume we have two source files, called <code>A.py</code> and <code>B.py</code>, their contents are as below:</p>
  
  <p><code>A.py</code></p>
  
  <pre><code>import B
  
  __global__ = 'not set yet'
  
  class Data:
      __var__ = 'not set yet'
  
  def init():
      global __global__
      __global__ = 'value set for global'
      Data.__var__ = 'value set for class var'
  
  if __name__ == '__main__':
      init()
  
      t = B.Test()
      t.display()
  
      print '============================'
      print 'global(in A):    ' + __global__
      print 'class var(in A): ' + Data.__var__
  </code></pre>
  
  <p><code>B.py</code></p>
  
  <pre><code>import A
  
  class Test:
      def __init__(self):
          self.glo = A.__global__
          self.var = A.Data.__var__
  
      def display(self):
          print 'global:          ' + self.glo
          print 'class var:       ' + self.var
          print '============================'
          print 'global(in B):    ' + A.__global__
          print 'class var(in B): ' + A.Data.__var__
  </code></pre>
  
  <p>then I ran <code>python A.py</code>, the output is as following:</p>
  
  <pre><code>global:          not set yet
  class var:       not set yet
  ============================
  global(in B):    not set yet
  class var(in B): not set yet
  ============================
  global(in A):    value set for global
  class var(in A): value set for class var
  </code></pre>
  
  <p>In my opinion, the first 4 outputs should coincide with the last 2 outputs, but the fact is not, seems the value is set during class and method definition and cannot be changed. It is very different from many other languages, such as Java. So, could any one help explain this, or paste me some links to help understand it? And is there any workaround to solve this?</p>
  
  <p>Thanks in advance,</p>
  
  <p>Kelvin</p>
  
  <p>==============edit==============</p>
  
  <p>Thanks @icktoofay, @hop and @RocketDonkey, after I tried the code below, I do find the root cause:</p>
  
  <pre><code>import sys
  import A
  import __main__
  
  __global__ = 'not set yet'
  
  class Data:
      __var__ = 'not set yet'
  
  def init():
      global __global__
      __global__ = 'value set for global'
      Data.__var__ = 'value set for class var'
  
  if __name__ == '__main__':
      init()
  
      for key in sys.modules.keys():
          if key in ['__main__', 'A']:
              print key + ' : ' + sys.modules[key].__file__
      print '===================='
      print 'A: ' + A.__global__
      print 'A: ' + A.Data.__var__
      print '===================='
      print __main__.__global__
      print __main__.Data.__var__
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>__main__ : A.py
  A : D:\test\python\A.py
  ====================
  A: not set yet
  A: not set yet
  ====================
  value set for global
  value set for class var
  </code></pre>
  
  <p>It is because file <code>A.py</code> was imported twice, one is named <code>__main__</code> and another is named <code>A</code>, the values were changed in module <code>__main__</code>, but for module <code>B</code>, the value is got from module <code>A</code>, so the value is not changed.</p>
  
  <p>I do need to dive deeper for module importing of python. :-D</p>