类方法classmethod 和静态方法staticmethod

Posted sdosun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类方法classmethod 和静态方法staticmethod相关的知识,希望对你有一定的参考价值。

类方法

 

classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。

 

比如在class 类下 使用@classmethod 定义一个函数  一般只需要  class名.方法()直接使用:

 

技术图片

 

 

 

静态方法

此方法一般不强制要求传递参数!

 技术图片

 

以上是关于类方法classmethod 和静态方法staticmethod的主要内容,如果未能解决你的问题,请参考以下文章

面试必问python实例方法类方法@classmethod静态方法@staticmethod和属性方法@property区别

开发者笔记python中的类方法(@classmethod)和静态方法(@staticmethod)

Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

python-静态方法staticmethod类方法classmethod属性方法property

python中静态方法(@staticmethod)和类方法(@classmethod)的区别

python笔记60 - 类里面静态方法(@staticmethod),类方法(@classmethod)和实例方法(self)的使用与区别