title: Python closures in a loop print same output
body: <p>Consider the following piece of code:</p>
  <pre><code>closures = {}
  
  for i in range(5):
    def closure():
      print(i)
    closures[i] = closure
  
  for k,_ in closures.items():
    closures[k]()
  </code></pre>
  <p>Intuitively I would expect the output to be (newline separated) &quot;0 1 2 3 4&quot;. But the output is actually &quot;4 4 4 4 4&quot; (each number on a new line).</p>
  <p>My question is, why does it do that and how can I get it to print what I would expect?</p>