首页 > 编程语言 > 详细

知识点 - python 装饰器@staticmethod和@classmethod区别和使用

时间:2018-02-27 20:47:50      阅读:194      评论:0      收藏:0      [点我收藏+]

1.通常来说,我们使用一个类的方法时,首先要实例化这个类,再用实例化的类来调用其方法

class Test(object):
    """docstring for Test"""
    def __init__(self, arg=None):
        super(Test, self).__init__()
        self.arg = arg

    def say_hi(self):
        print(hello wrold)

def main():
    test = Test() #//1. 首先实例化test类
    test.say_hi() #//2. 再调用类的方法

if __name__ == __main__:
    main()

输出

hello wrold

2.而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。

这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。

class Test(object):
    name = stephen

    """docstring for Test"""
    def __init__(self, arg=None):
        super(Test, self).__init__()
        self.arg = arg

    def say_hi(self):
        print([1]hello wrold)

    @staticmethod
    def say_bad():
        print([2]say bad)
        print([3]Test.name:%s%Test.name)  #//不需要实例化
        Test().say_hi()                     #//先要实例化,才能调用say_hi()

    @classmethod
    def say_good(cls):
        print([4]say good)
        print([5]cls.name:%s%cls.name)    #//不需要实例化
        cls.say_bad()                       #//不需要实例化
        cls().say_hi()                      #//先要实例化,才能调用say_hi()

def main():
    Test.say_bad()
    print("----------------------------------")
    Test.say_good()

if __name__ == __main__:
    main()

输出结果

[2]say bad
[3]Test.name:stephen
[1]hello wrold
----------------------------------
[4]say good
[5]cls.name:stephen
[2]say bad
[3]Test.name:stephen
[1]hello wrold
[1]hello wrold

3.注意我们在定义这些方法时的区别

  1. 类的普通方法,第一个参数需要self参数表示自身。
  2. @staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
  3. @classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。

4.方法内部

  1. 如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
  2. 而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。
  3. 如果需要调用带self的方法,必须得先对类进行实例化。
class Test(object):
    name = stephen

    """docstring for Test"""
    def __init__(self, arg=None):
        super(Test, self).__init__()
        self.arg = arg

    def say_hi(self):
        print([1]hello wrold)

    @staticmethod
    def say_bad():
        print([2]say bad)
        print([3]Test.name:%s%Test.name) #  //不需要实例化
        Test().say_hi()                    #  //先要实例化,才能调用say_hi()

    @classmethod
    def say_good(cls):
        print([4]say good)
        print([5]cls.name:%s%cls.name)   #  //不需要实例化
        cls.say_bad()                      #  //不需要实例化
        cls().say_hi()                     #  //先要实例化,才能调用say_hi()

def main():
    Test.say_bad()
    print("----------------------------------")
    Test.say_good()

if __name__ == __main__:
    main()

输出结果

[2]say bad
[3]Test.name:stephen
[1]hello wrold
----------------------------------
[4]say good
[5]cls.name:stephen
[2]say bad
[3]Test.name:stephen
[1]hello wrold
[1]hello wrold

 

知识点 - python 装饰器@staticmethod和@classmethod区别和使用

原文:https://www.cnblogs.com/caimengzhi/p/8480302.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!