title: Namespace and scope in Python
body: <p>I have run into a script concerning namespace and scope in Python, for which I can’t figure out how this script runs because of its mixed and recondite usage of these two concepts. Here is the code:</p>
  
  <pre><code>j, k = 1,2
  def proc1():
         j, k = 3, 4
         print “ j == %d and k == %d” % (j, k)
         k = 5
  
  def proc2():
         j = 6
         proc1()
         print “ j == %d and k == %d” %(j , k)
  
  k = 7
  proc1()
  print “ j == %d and k == %d” % (j, k)
  
  j = 8
  proc2()
  print “ j == %d and k == %d” % (j, k)
  </code></pre>
  
  <p>I reckon the output of this script should contain only four print expression, but it turns out to be five when running it . Besides, the value of j and k in each line is also quite different from what I have expected. Could someone explain how this runs?</p>
  
  <p>Sincere gratitude offered if you could also elaborate on the namespace and scope in these chunk of code. Besides, here is the output when I run it from my computer which currently is equipped with Python 2.7.14.
  <a href="https://i.stack.imgur.com/Gxwm0.jpg" rel="nofollow noreferrer">output result</a>]<a href="https://i.stack.imgur.com/Gxwm0.jpg" rel="nofollow noreferrer">1</a></p>