python hasattr/getattr/setattr/instancemethod/classmethod/staticmethod

描述:含hasattr/getattr/setattr/instancemethod/classmethod/staticmethod

instancemethod: 普通方法,供实例调用

classmethod: 类方法,不需实例化对象,供实例和类调用

staticmethod: 与类中其他方法、属性无关的方法,供类、实例调用

 

# -*- coding: utf8 -*-


class First(object):
    name = 'first class name'

    def instance_func(self):
        print("class first instance func")

    @classmethod
    def class_func(cls):
        print('class first class func')
        print('class first attr name: ', cls.name)

    @staticmethod
    def statuc_func():
        print('class first static func')


if __name__ == '__main__':
    a = First()
    print("* * " * 30)
    print(hasattr(a, 'name'))
    print(getattr(a, 'name'))
    print(setattr(a, 'name', '123'))
    print(getattr(a, 'name'))

    print("* * " * 30)
    a.instance_func()
    a.class_func()
    a.statuc_func()
    First.class_func()
    First.statuc_func()
    print("* * "*30)

输出:

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
True
first class name
None
123
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
class first instance func
class first class func
class first attr name:  first class name
class first static func
class first class func
class first attr name:  first class name
class first static func
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值