一、单例模式

a、单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现

b、类实现如下:

class Sigletion(objects):

import time

def __init__(self):

time.sleep(1)

@classmethod

def instance(cls,*args,**kwargs)

if not hasattr(Sigletion,'_instance'):

Sigletion._instance=Sigletion(*args,**kwargs)

return Sigletion._instance

import threading

daf task(arg):

obj=Sigletion.instance()

print(obj)

for i in range(10):

t=threading.Thread(target=task,args=[i,])

t.start()

c、基于__new__方法实现单例模式

import time

import threading

class Singleton(object):

_instance_lock=threading.Lock()

def __init__(self):

pass

def __new__(cls, *args, **kwargs):

if not hasattr(Singleton,"_instance"):

with Singleton._instance_lock:

if not hasattr(Singleton,"_instance"):

Singleton._instance=object.__new__(cls,*args,**kwargs)

return Singleton._instance

obj1=Singleton()

obj2=Singleton()

print(obj1,obj2)

def task(arg):

obj = Singleton()

print(obj)

for i in range(10):

t = threading.Thread(target=task,args=[i,])

t.start()

d、基于metaclass方式实现单例模式

"""

1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法

2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】

class Foo:

def __init__(self):

pass

def __call__(self, *args, **kwargs):

pass

# 第1步: 执行type的 __call__ 方法

# 1.1 调用 Foo类(是type的对象)的 __new__方法,用于创建对象。

# 1.2 调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。

obj = Foo()

# 第2步:执行Foodef __call__ 方法

obj()

"""

import threading

class SingletonType(type):

_instace_lock=threading.Lock()

def __call__(cls, *args, **kwargs):

if not hasattr(cls, "_instance"):

with SingletonType._instace_lock:

if not hasattr(cls, "_instance"):

cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)

return cls._instance

class Foo(metaclass=SingletonType):

def __init__(self,name):

self.name=name

obj1 = Foo('name')

obj2 = Foo('name')

print(obj1,obj2)

python单例模式数据库连接失败_Python中单例模式总结相关推荐

  1. python单例模式数据库连接池_Python实现单例模式的四种方式

    # 单例模式实现方式一:类方法import settings​class MySQL:__instance=Nonedef __init__(self, ip, port):self.ip = ips ...

  2. python中单例模式是什么_Python中单例模式总结

    一.单例模式 a.单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现 b.类实现如下: class Sigletion(objects): import t ...

  3. python安装pyecharts失败_Python中pyecharts安装及安装失败的解决方法

    pyecharts 是一个用于生成 Echarts 图表的类库.Echarts 是百度开源的一个数据可视化 JS 库.这篇文章重点给大家介绍pyecharts安装失败的处理方法,具体详情如下: pye ...

  4. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  5. python中单例模式是什么_python中的单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 比如,某 ...

  6. python什么是单例模式_Python中单例模式是什么

    Python中单例模式是什么 发布时间:2020-07-20 14:14:20 来源:亿速云 阅读:61 作者:清晨 这篇文章将为大家详细讲解有关Python中单例模式是什么,小编觉得挺实用的,因此分 ...

  7. python基础知识测试题_Python中的单元测试—基础知识

    python基础知识测试题 Unit testing is the number one skill which separates people who just finished their de ...

  8. python实现数据库连接池_Python实现Mysql数据库连接池

    Python实现Mysql数据库连接池 python连接Mysql数据库: python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都 ...

  9. python方法之间加点_python中技巧

    1.使用xpath从html文档得到其中元素: 123we为了得到其中的123we元素 tree=html.fromstring(***.text) tt=list(set(tree.xpath(&q ...

最新文章

  1. shell整理(41)====判断输入是不是ip
  2. vmware下ubuntu安装vmware tool工具及使用鼠标滚轮的方法
  3. springboot封装统一查询对象进行多条件查询案例(mybatis和mybatis-plus+反射两种版本)
  4. postgresql定义访问ip与用户_PostgreSQL 设置允许访问IP的操作
  5. QT Core | 信号槽03 - 自定义信号与槽
  6. phpstudy免费安全检测服务_@你,您有一份免费安全服务已到账
  7. mysql实验视图及索引_MySQL视图及索引
  8. 显卡显存故障检测工具_为RTX30系显卡做准备,骨伽GEX750金牌全模组电源装机体验...
  9. Android画一条横线
  10. 脚本故事 - 2003年11月
  11. 评:网瘾不是问题 两代人文化冲突是根本
  12. Three.js的人物动画与交互
  13. 堆内存和栈内存的区别
  14. vim设置(非常全面),即.vimrc文件的配置
  15. 什么是Type-c降噪耳机?type-c接口耳机降噪方案
  16. 引导路径动画 (2)
  17. mysql selecte_【mysql】MySQL eplain 完全解读
  18. C++虚函数表解析 (Lawliet 修改+注释版)(附有部分网友的重要评论)
  19. wgt包更新时会下载但是不会安装
  20. linux判断目录是否存在命令,linux shell 中判断文件、目录是否存在的方法

热门文章

  1. MYS-6ULX-IOT 开发板测评——面向高端物联网的极具性价比解决方案
  2. 全能系统优化组合软件WinTools安装激活教程
  3. css有哪些选择器,优先级如何计算?
  4. 朋友圈的设计及实现。
  5. 【七月】再见,不负遇见
  6. 右边maven中没有Dependencies
  7. git clone 分支
  8. egret---添加序列帧动画,帧包动画,龙骨动画
  9. 【MATLAB函数】function定义函数
  10. Python IDE简单测试推荐