python函数实例与类实例_方法对象vs函数对象,Python类实例vs类

本教程确实是错误的;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)

>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值