title: Lambda in a loop
body: <p>Considering the following code snippet:</p>
  <pre><code># directorys == {'login': &lt;object at ...&gt;, 'home': &lt;object at ...&gt;}
  for d in directorys:
      self.command[&quot;cd &quot; + d] = (lambda : self.root.change_directory(d))
  </code></pre>
  <p>I expect to create a dictionary of two function as following :</p>
  <pre><code># Expected :
  self.command == {
      &quot;cd login&quot;: lambda: self.root.change_directory(&quot;login&quot;),
      &quot;cd home&quot;: lambda: self.root.change_directory(&quot;home&quot;)
  }
  </code></pre>
  <p>but it looks like the two lambda function generated are exactly the same :</p>
  <pre><code># Result :
  self.command == {
      &quot;cd login&quot;: lambda: self.root.change_directory(&quot;login&quot;),
      &quot;cd home&quot;: lambda: self.root.change_directory(&quot;login&quot;)   # &lt;- Why login ?
  }
  </code></pre>
  <p>I really don't understand why. Do you have any suggestions ?</p>