title: Closures in a class scope
body: <p>From my understanding, function and class scopes behave pretty much the same:</p>
  
  <pre><code>&gt;&gt;&gt; def x():
  ...     a = 123
  ...     print (locals())
  ... 
  &gt;&gt;&gt; x()
  {'a': 123}
  
  
  &gt;&gt;&gt; class x():
  ...     a = 123
  ...     print (locals())
  ... 
  {'a': 123, '__module__': '__main__'}
  </code></pre>
  
  <p>When, however, I define a closure, behaviors are different. A function simply returns the local binding, as expected:</p>
  
  <pre><code>&gt;&gt;&gt; def x():
  ...     a = 123
  ...     t = lambda: a
  ...     return t
  ... 
  &gt;&gt;&gt; x()()
  123
  </code></pre>
  
  <p>whereas in a class the binding appears to be lost:</p>
  
  <pre><code>&gt;&gt;&gt; class x():
  ...     a = 123
  ...     t = lambda self: a
  ... 
  &gt;&gt;&gt; x().t()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 3, in &lt;lambda&gt;
  NameError: global name 'a' is not defined
  </code></pre>
  
  <p>Can anyone explain the discrepancy?</p>