Difference between python lambda functions inside class and def
<p>I have the following code.</p>

<pre><code>class SomeClass:

    a = lambda self: self.b()

    def __init__(self):
        self.b = lambda self: None

s = SomeClass()
s.a()
</code></pre>

<p>It give me "TypeError: () takes exactly 1 argument (0 given)". And I want to understand why.
<p>My explanation:</p>

<ul>
<li><p>a - class method, so <code>s.a()</code> equlas <code>SomeClass.a(s)</code></p></li>
<li><p>b - object's attribute (not a method, just a function), that is why <code>self.b()</code> doesn't equal <code>SomeClass.b(self)</code>
<p>So in <code>a = lambda self: self.b()</code> argument for <code>b</code> is missing.
<p>Am I  right? 
<p>P.S. Is it closure effect?</p>

<pre><code>class SomeClass:

    a = lambda self: self.b()

    def __init__(self):
        self.data = 12
        self.b = lambda: self.data

s = SomeClass()
print s.a()       #12
s.data = 24
print s.a()       #24
</code></pre></li>
</ul>

<p>Your problem here is the difference between bound methods and functions</p>

<p>Have a simpler example:</p>

<pre><code>class Someclass(object):
    bound = lambda *args: "bound method got {}".format(args)
    def __init__(self):
        self.unbound = lambda *args: "function got {}".format(args)
</code></pre>



<pre><code>&gt;&gt;&gt; c = Someclass()
</code></pre>

<p>If we look closely, these two functions are not of the same type:</p>

<pre><code>&gt;&gt;&gt; c.bound
&lt;bound method Someclass.&lt;lambda&gt; of &lt;__main__.Someclass object at 0x...&gt;&gt;
&gt;&gt;&gt; c.unbound
&lt;function Someclass.__init__.&lt;locals&gt;.&lt;lambda&gt; at 0x...&gt;
</code></pre>

<p>And as a result, when we call them, they receive different arguments:</p>

<pre><code>&gt;&gt;&gt; c.bound(1, 2, 3)
'bound method got (&lt;__main__.Someclass object at 0x...&gt;, 1, 2, 3)'
&gt;&gt;&gt; c.unbound(1, 2, 3)
'unbound got (1, 2, 3)'
</code></pre>

<p>Notice that only the "bound" function got a <code>self</code> argument passed in. </p>

<p><sup>Tested in 3.5 - 2.7 might have slightly different names for things</sup></p>

python oop lambda