Python中staticmethod和classmethod的作用与区别
作者:斜阳雨陌 时间:2022-03-12 21:30:00
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法。
而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。
这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。
既然@staticmethod和@classmethod都可以直接类名.方法名()来调用,那他们有什么区别呢
从它们的使用上来看
@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
@classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。
要明白,什么是实例方法、静态方法和类方法:
class Demo(object):
def instance_method(self, your_para):
"""
this is an instance_method
you should call it like the follow:
a = Demo()
a.instance_method(your_para)
plus: in python, we denote 'cls' as latent para of Class
while 'self' as latent para of the instance of the Class
:param your_para:
:return:
"""
print("call instance_method and get:", your_para)
@classmethod
def class_method(cls, your_para):
"""
this is a class_method
you can call it like the follow:
method1:
a = Demo()
a.class_method(your_para)
method2:
Demo.class_method
plus: in python, we denote 'cls' as latent para of Class
while 'self' as latent para of the instance of the Class
:param your_para:
:return:
"""
print("call class_method and get:", your_para)
@staticmethod
def static_method(your_para):
"""
this is a static_method and you can call it like the
methods of class_method
:param your_para:
:return:
"""
print("call static_method and get:", your_para)
虽然类方法在调用的时候没有显式声明cls,但实际上类本身是作为隐含参数传入的。这就像实例方法在调用的时候也没有显式声明self,但实际上实例本身是作为隐含参数传入的。
对于静态函数,我们一般把与类无关也与实例无关的函数定义为静态函数。例如入口检查的函数就最好定义成静态函数。
类方法的妙处, 在继承中的作用:
class Fruit(object):
total = 0 # 这是一个类属性
@classmethod
def print_total(cls):
print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls是类本身,打印类属性total的值
print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total))
print("=======================")
@classmethod
def set(cls, value):
cls.total = value
class Apple(Fruit):
pass
class Orange(Fruit):
pass
app1 = Apple()
app1.set(10)
app1.print_total()
Apple.print_total()
Fruit.set(2)
app1.print_total()
Fruit.print_total()
"""
output:
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 0 and its id: 1355200944
=======================
this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264
this is the Fruit.total: 2 and its id: 1355201008
=======================
this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008
this is the Fruit.total: 2 and its id: 1355201008
=======================
"""
来源:https://blog.csdn.net/qq_15037231/article/details/77943109
标签:python,staticmethod,classmethod


猜你喜欢
详细分析vue表单数据的绑定
2023-07-02 16:27:51

Mootools 1.2教程(8)——输入过滤第一部分(数字)
2008-11-27 13:01:00
php实现将数组或对象写入到文件的方法小结【三种方法】
2023-11-19 05:08:04
python多进程间通信代码实例
2023-10-06 20:22:17

MySQL5.6基本优化配置
2024-01-26 07:31:15
javascript jQuery $.post $.ajax用法
2024-04-16 08:52:52
微信小程序实现点击出现弹窗
2024-04-19 10:14:43
JS限制文本框只能输入数字和字母方法
2023-07-02 05:25:43
Python创建对称矩阵的方法示例【基于numpy模块】
2021-01-03 07:35:59

基于python实现操作redis及消息队列
2021-08-06 22:31:29
解析mysql 缓存如何使用内存
2024-01-19 05:21:32
javascript实现文本框标签验证的实例代码
2024-04-25 13:07:41
Python matplotlib超详细教程实现图形绘制
2023-04-20 06:37:53

python RabbitMQ 使用详细介绍(小结)
2022-11-18 21:29:33
浅谈vue异步数据影响页面渲染
2024-04-30 10:45:19

把论坛从ACCESS转成SQL版本
2009-04-13 15:59:00
cv2.getStructuringElement()函数及开、闭、腐蚀、膨胀原理讲解
2023-08-24 23:07:23

Sql Server2012 使用IP地址登录服务器的配置图文教程
2024-01-26 18:27:25

IE 浏览器安全级别详情及区别小结
2022-05-28 20:24:16
解析mysql修改为utf8后仍然有乱码的问题
2024-01-14 14:36:09