python中的class_static的@classmethod的巧妙用法
作者:peterpan--1998 时间:2022-07-18 15:54:43
python中的class_static的@classmethod的使用 classmethod的使用,主要针对的是类而不是对象,在定义类的时候往往会定义一些静态的私有属性,但是在使用类的时候可能会对类的私有属性进行修改,但是在没有使用class method之前对于类的属性的修改只能通过对象来进行修改,这是就会出现一个问题当有很多对象都使用这个属性的时候我们要一个一个去修改对象吗?答案是不会出现这么无脑的程序,这就产生classmethod的妙用。请看下面的代码:
class Goods:
__discount = 0.8
def __init__(self,name,money):
self.__name = name
self.__money = money
@property
def price(self):
return self.__money*Goods.__discount
@classmethod
def change(cls,new_discount):#注意这里不在是self了,而是cls进行替换
cls.__discount = new_discount
apple = Goods('苹果',5)
print(apple.price)
Goods.change(0.5) #这里就不是使用apple.change()进行修改了
print(apple.price)
上面只是简单的列举了class method的一种使用场景,后续如果有新的会持续更新本篇文章 2.既然@staticmethod和@classmethod都可以直接类名.方法名()来调用,那他们有什么区别呢
从它们的使用上来看,
@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
@classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。
下面上代码。
class A(object):
bar = 1
def foo(self):
print 'foo'
@staticmethod
def static_foo():
print 'static_foo'
print A.bar
@classmethod
def class_foo(cls):
print 'class_foo'
print cls.bar
cls().foo()
###执行
A.static_foo()
A.class_foo()
知识点扩展:python classmethod用法
需求:添加类对象属性,在新建具体对象时使用该变量
class A():
def __init__(self,name):
self.name = name
self.config = {'batch_size':A.bs}
@classmethod
def set_bs(cls,bs):
cls.bs = bs
def print_config(self):
print (self.config)
A.set_bs(4)
a = A('test')
a.print_config()
来源:https://blog.csdn.net/qq_42617984/article/details/117914377
标签:python,classmethod,class,static
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在js中的replace方法详解
2007-08-21 15:47:00
python实现转圈打印矩阵
2022-10-28 09:23:20
PHP getDocNamespaces()函数讲解
2023-06-13 22:19:06
![](https://img.aspxhome.com/file/2023/5/55405_0s.png)
Python3实现从文件中读取指定行的方法
2021-01-06 04:18:17
Div+CSS网页布局对SEO的影响漫谈
2008-08-22 12:58:00
在python中list作函数形参,防止被实参修改的实现方法
2022-11-15 19:27:25
![](https://img.aspxhome.com/file/2023/7/87777_0s.jpg)
python3 wechatpy微信支付的项目实践
2023-08-29 14:13:36
![](https://img.aspxhome.com/file/2023/5/87975_0s.jpg)
ASP使用FSO组件生成HTML静态页面
2007-10-15 12:19:00
python实现简单的学生管理系统
2022-11-25 08:34:04
![](https://img.aspxhome.com/file/2023/7/112747_0s.jpg)
block 和 inline 的区别是?
2009-12-08 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
mysql 8.0.13 安装配置方法图文教程
2024-01-14 15:20:52
![](https://img.aspxhome.com/file/2023/4/115294_0s.png)
PHP图像识别技术原理与实现
2024-06-05 09:43:54
![](https://img.aspxhome.com/file/2023/1/122501_0s.png)
python神经网络Keras实现LSTM及其参数量详解
2023-02-09 14:02:22
![](https://img.aspxhome.com/file/2023/1/70701_0s.jpg)
django反向解析URL和URL命名空间的方法
2022-05-28 20:35:09
javaScript 删除字符串空格多种方法小结
2024-05-02 16:10:33
讲解Access数据库中数据表的自动重新联接
2008-11-28 16:29:00
Php中文件下载功能实现超详细流程分析
2023-10-07 17:09:13
![](https://img.aspxhome.com/file/2023/2/55762_0s.png)
Python脚本导出为exe程序的方法
2022-08-22 21:33:05
![](https://img.aspxhome.com/file/2023/3/121453_0s.png)
python 解决mysql where in 对列表(list,,array)问题
2024-01-27 06:34:23
ORACLE 数据库RMAN备份恢复
2009-04-24 12:23:00