Docs
python cls_python中的cls到底指的是什么

python cls_python中的cls到底指的是什么

链接:https://www.zhihu.com/question/49660420/answer/335991541

一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法。

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

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

class A(object):
    a = 'a'
    @staticmethod
    def foo1(name):
        print ('hello', name)
    def foo2(self, name):
        print ('hello', name)
    @classmethod
    def foo3(cls, name):
        print ('hello', name)
        cls().foo2(name)
        print('slef')


if __name__ == "__main__":
    pass
    # a= A()
    # a.foo1('studyquant') # 输出: hello mamq
    # A.foo1('studyquant')# 输出: hello mamq

    A.foo3('cls studyquant') # 输出: hello mamq
    # A.foo1('studyquant')# 输出: hello mamq
    
    
# 测试结果
hello cls studyquant
hello cls studyquant
slef

但是通过例子发现staticmethod与classmethod的使用方法和输出结果相同,再看看这两种方法的区别。

既然@staticmethod和@classmethod都可以直接类名.方法名()来调用,那他们有什么区别呢

从它们的使用上来看,
@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
@classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。

也就是说在classmethod中可以调用类中定义的其他方法、类的属性,但staticmethod只能通过A.a调用类的属性,但无法通过在该函数内部调用A.foo2()。修改上面的代码加以说明: