Python下简易的单例模式详解
作者:whyfive4 时间:2022-02-15 10:03:19
Python 下的单例模式
要点:
1.某个类只能有一个实例;
2.它必须自行创建这个实例;
3.它必须自行向整个系统提供这个实例
方法:重写new函数
应该考虑的情况:
1.这个单例的类可能继承了别的类
2.这个单例的类还有可能要接收参数来实例化
要点:
实例化的过程其实不是直接调用init的,首先是new分配一块空间来创建实例,再由init对这个实例进行初始化.我们无法阻止new和init的调用,我们只能是限制他们的内容,以此使他们能达到单例的目的
代码:
class people(object):
def __new__(cls,*args,**kargs):
return super(people,cls).__new__(cls)
def __init__(self,name):
self.name = name
def talk(self):
print("hello,I am %s" %self.name)
class student(people):
def __new__(cls,*args,**kargs):
if not hasattr(cls,"instance"):
cls.instance = super(student,cls).__new__(cls,*args,**kargs)
return cls.instance
a = student("Timo")
print(a)
b = student("kysa")
c = student("Luyi")
a.talk()
b.talk()
print(c)
这里的输出结果是:
<__main__.student object at 0x0000025AC48BF2E8>
hello,I am Luyi
hello,I am Luyi
<__main__.student object at 0x0000025AC48BF2E8>
可以确定的是: 确实是单例了,因为a的id和b,c的id是一致的
但是为什么:a先创建明明是Timo,可是为什么a的name变成了Luyi呢?
原因:
虽然确实是a这个实例,但是在最后c重新调用了new,返回了a的实例,再经过init,改变了a的属性,执行时name ->Luyi.
解决:
这种情况下,我们只需要设置类变量,让init在类变量的限制下,只对类进行一次有效的初始化.
代码:
class people(object):
def __new__(cls,*args,**kargs):
return super(people,cls).__new__(cls)
def __init__(self,name):
self.name = name
def talk(self):
print("hello,I am %s" %self.name)
class student(people):
def __new__(cls,*args,**kargs):
if not hasattr(cls,"instance"):
cls.instance = super(student,cls).__new__(cls,*args,**kargs)
return cls.instance
def __init__(self,name):
if not hasattr(self,"init_fir"):
self.init_fir = True
super(student,self).__init__(name)
a = student("Timo")
print(a)
b = student("kysa")
c = student("Luyi")
a.talk()
b.talk()
print(c)
好了,到这里就用Python实现了一个简易的单例模式.
以上所述是小编给大家介绍的Python下简易的单例模式详解整合网站的支持!
来源:https://blog.csdn.net/whyfive4/article/details/89048773
标签:Python,单例模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
对Tensorflow中tensorboard日志的生成与显示详解
2023-07-19 20:00:44
![](https://img.aspxhome.com/file/2023/7/87547_0s.jpg)
selenium自动化测试简单准备
2023-02-07 13:04:12
![](https://img.aspxhome.com/file/2023/4/75904_0s.png)
Python读取系统文件夹内所有文件并统计数量的方法
2021-11-14 07:39:28
利用python Selenium实现自动登陆京东签到领金币功能
2021-11-09 12:00:33
![](https://img.aspxhome.com/file/2023/8/72258_0s.jpg)
Python queue模块功能大全
2023-02-23 23:42:27
js求一组数中的最大数
2008-04-10 12:00:00
基于Python实现对比Exce的工具
2022-12-04 17:44:44
![](https://img.aspxhome.com/file/2023/4/70894_0s.png)
php中fgetcsv()函数用法实例
2023-06-15 05:50:37
详解Python如何轻松实现定时执行任务
2022-02-02 16:44:42
![](https://img.aspxhome.com/file/2023/5/88995_0s.png)
详谈Python基础之内置函数和递归
2021-12-19 21:55:40
![](https://img.aspxhome.com/file/2023/2/75952_0s.png)
纯ASP结合VML生成完美图-柱图
2010-05-11 16:48:00
MySQL转义字符
2011-06-19 16:06:04
Django与DRF结合的全局异常处理方案详解
2021-05-19 22:53:16
表格头固定而列可滚动的效果
2020-08-11 21:28:41
基于python分享极坐标下的几类典型曲线
2023-05-02 18:18:37
![](https://img.aspxhome.com/file/2023/6/103326_0s.png)
Python数据存储之 h5py详解
2023-09-18 00:04:48
打造设计你自己的字体 Ⅲ
2008-03-14 07:52:00
![](https://img.aspxhome.com/file/UploadPic/20083/14/200831475626244s.gif)
python selenium 获取标签的属性值、内容、状态方法
2021-03-12 23:02:46
sql 附加数据库后,孤立用户的解决办法
2011-01-22 13:46:00
![](https://img.aspxhome.com/file/UploadPic/20111/22/4-37s.jpg)
有关JS中Event对象的几点总结
2009-03-06 12:36:00