擅长:python、mysql、java
<p>您将看到Python幕后机制的一些效果。在</p>
<p>当您编写<code>f = MyClass.__dict__['mymethodname']</code>时,您将得到“mymethodname”的原始实现,这是一个普通函数。要调用它,需要传入一个附加参数class instance。在</p>
<p>当您编写<code>f = MyClass.mymethodname</code>(注意mymethodname后面没有括号),您将得到一个类MyClass的未绑定方法,它是<code>MethodType</code>的一个实例,它包装了上面获得的原始函数。要调用它,需要传入一个附加参数class instance。在</p>
<p>当您编写<code>f = MyClass().mymethodname</code>(请注意,在使用MyClass的方法之前,我已经创建了一个类MyClass的对象),您将得到一个类MyClass实例的绑定方法。您不需要向它传递额外的类实例,因为它已经存储在其中。在</p>
<p>{{a1>用一个名为^的非绑定}方法来命名。例如:</p>
<pre><code>unbound_mth = getattr(MyClass, "mymethodname")
</code></pre>
<p>或者</p>
^{pr2}$