python2与3中的function与method

class T(object):
    def func(self):
        pass
    @staticmethod
    def sfunc(self):
        pass
    @classmethod
    def cfunc(cls):
        pass

一、python2

实例方法
T.func
<unbound method T.func>
T.func.__class__
<type 'instancemethod'>
T.__dict__["func"]
<function func at 0x0000000002DB8D68>
T.__dict__["func"].__class__
<type 'function'>

静态方法
T.sfunc
<function sfunc at 0x0000000002DB8EB8>
T.sfunc.__class__
<type 'function'>
T.__dict__["sfunc"]
<staticmethod object at 0x0000000002E1CF48>
T.__dict__["sfunc"].__class__
<type 'staticmethod'>

类方法
T.cfunc
<bound method type.cfunc of <class '__main__.T'>>
T.cfunc.__class__
<type 'instancemethod'>
T.__dict__["cfunc"]
<classmethod object at 0x0000000002DE3B58>
T.__dict__["cfunc"].__class__
<type 'classmethod'>

t=T()
实例方法
t.func
<bound method T.func of <__main__.T object at 0x0000000002E32DD8>>
t.func.__class__
<type 'instancemethod'>
静态方法
t.sfunc
<function sfunc at 0x0000000002DB8EB8>
t.sfunc.__class__
<type 'function'>
类方法
t.cfunc
<bound method type.cfunc of <class '__main__.T'>>
t.cfunc.__class__
<type 'instancemethod'>

二、python3

实例方法
T.func和T.__dict__["func"]均返回function
T.func
<function T.func at 0x00000000055B28C8>
T.func.__class__
<class 'function'>
T.__dict__["func"]
<function T.func at 0x00000000055B28C8>
T.__dict__["func"].__class__
<class 'function'>
静态方法
T.sfunc
<function T.sfunc at 0x00000000055B2950>
T.sfunc.__class__
<class 'function'>
T.__dict__["sfunc"]
<staticmethod object at 0x000000000557B198>
T.__dict__["sfunc"].__class__
<class 'staticmethod'>
类方法
T.cfunc
<bound method T.cfunc of <class '__main__.T'>>
T.cfunc.__class__
<class 'method'>
T.__dict__["cfunc"]
<classmethod object at 0x00000000055B9BE0>
T.__dict__["cfunc"].__class__
<class 'classmethod'>




t=T()
实例方法
t.func
<bound method T.func of <__main__.T object at 0x00000000055B9EB8>>
t.func.__class__
<class 'method'>
静态方法
t.sfunc
<function T.sfunc at 0x00000000055B2950>
t.sfunc.__class__
<class 'function'>
类方法
t.cfunc
<bound method T.cfunc of <class '__main__.T'>>
t.cfunc.__class__
<class 'method'>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值