本教程确实是错误的;class.functionname和instance.functionname都返回一个方法对象。
接下来是函数是一个descriptor,调用它们的__get__方法,返回一个方法。方法具有指向原始函数的__func__属性:>>> class Foo(object):
... def bar(self):
... pass
...
>>> Foo.bar
>>> Foo().bar
>
>>> # accessing the original function
...
>>> Foo.bar.__func__
>>> # turning a function back into a method
...
>>> Foo.bar.__func__.__get__(None, Foo)
>>> Foo.bar.__func__.__get__(Foo(), Foo)
>
但这一切在Python 3中都发生了变化;这里Foo.bar返回函数本身,未绑定的方法不再存在:$ python3.3
Python 3.3.0 (default, Sep 29 2012, 08:16:08)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo:
... def bar(self):
... pass
...
>>> Foo.bar
>>> Foo.bar.__get__(None, Foo)
>>> Foo.bar.__get__(Foo(), Foo)
>