<p>您应该注意到实例方法不是lambda。你知道吗</p>
<p>例如,让我们在IPython中做一个简单的实验</p>
<pre><code>In [12]: class A:
....: def f(self):
....: return 1
....:
In [13]: A.f.__class__
Out[13]: instancemethod
In [14]: another_f = lambda self: 1
In [15]: another_f.__class__
Out[15]: function
</code></pre>
<p>试图将属性绑定到<code>lambda</code>会在调用它时失败。你知道吗</p>
<pre><code>In [27]: an_instance = A()
In [28]: an_instance.g = lambda self: 2
In [29]: an_instance.g()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-5122c91d3e8f> in <module>()
----> 1 an_instance.g()
TypeError: <lambda>() takes exactly 1 argument (0 given)
</code></pre>
<p>相反,您应该做的是用<code>types.MethodType</code>包装lambda</p>
<pre><code>In [31]: an_instance.g = types.MethodType(lambda self: 2, an_instance)
In [32]: an_instance.g()
Out[32]: 2
</code></pre>
<p>在<code>descriptors</code>背后发生了一些奇怪的魔法。在我看来,这不是一个很好的解决方案,但是。。。如果你想了解更多,这里有一个链接<a href="http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html" rel="nofollow">http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html</a></p>