Classmethod 修饰符
跳到导航
跳到搜索
Classmethod是什么鬼
cat classmeth.py #!/usr/bin/python # -*- coding: utf-8 -*- class A(object): bar = 1 def func1(self): print ('foo') @classmethod def func2(cls): print ('func2') print (cls.bar) cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化 evan@latop:~/python//pytest$ python classmeth.py func2 1 foo evan@latop:~/python/py2018/pytest$ cat 2meth.py #!/usr/bin/python # -*- coding: utf-8 -*- class A(object): bar = 'love' def func1(self): print ('2love') @classmethod def func2(cls): print ('func2') print (cls.bar) cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化 python 2meth.py func2 love 2love
see also
python - @staticmethod和@classmethod的作用与区别
https://realpython.com/instance-class-and-static-methods-demystified/