方法一

  1. import threading
  2. class Singleton(object):
  3. __instance = None
  4. __lock = threading.Lock()   # used to synchronize code
  5. def __init__(self):
  6. "disable the __init__ method"
  7. @staticmethod
  8. def getInstance():
  9. if not Singleton.__instance:
  10. Singleton.__lock.acquire()
  11. if not Singleton.__instance:
  12. Singleton.__instance = object.__new__(Singleton)
  13. object.__init__(Singleton.__instance)
  14. Singleton.__lock.release()
  15. return Singleton.__instance

1.禁用__init__方法,不能直接创建对象。

2.__instance,单例对象私有化。

3.@staticmethod,静态方法,通过类名直接调用。

4.__lock,代码锁。

5.继承object类,通过调用object的__new__方法创建单例对象,然后调用object的__init__方法完整初始化。

6.双重检查加锁,既可实现线程安全,又使性能不受很大影响。

方法二:使用decorator

  1. #encoding=utf-8
  2. def singleton(cls):
  3. instances = {}
  4. def getInstance():
  5. if cls not in instances:
  6. instances[cls] = cls()
  7. return instances[cls]
  8. return getInstance
  9. @singleton
  10. class SingletonClass:
  11. pass
  12. if __name__ == '__main__':
  13. s = SingletonClass()
  14. s2 = SingletonClass()
  15. print s
  16. print s2

也应该加上线程安全

附:性能没有方法一高

  1. import threading
  2. class Sing(object):
  3. def __init__():
  4. "disable the __init__ method"
  5. __inst = None # make it so-called private
  6. __lock = threading.Lock() # used to synchronize code
  7. @staticmethod
  8. def getInst():
  9. Sing.__lock.acquire()
  10. if not Sing.__inst:
  11. Sing.__inst = object.__new__(Sing)
  12. object.__init__(Sing.__inst)
  13. Sing.__lock.release()
  14. return Sing.__inst

Python单例模式研究的更多相关文章

  1. python 单例模式获取IP代理

    python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...

  2. Python 单例模式讲解

    Python 单例模式讲解 本节内容: classmethod用途 单例模式方法一 类__new__方法讲解 单例模式方法二 前言: 使用单例方法的好处:对于一个类,多次实例化会产生多个对象,若使用单 ...

  3. python单例模式的实现与优化

    python单例模式的实现与优化 阅读目录(Content) 单例模式 实现单例模式的几种方式 1.使用模块 2.使用装饰器 3.使用类 4.基于__new__方法实现(推荐使用,方便) 5.基于me ...

  4. Python单例模式的设计与实现【完美版】

    目录 1. 按 2. 本文地址 3. 通过继承单例父类来实现 4. 使用装饰器实现 4.1. 懒汉式 4.2. 饿汉式 4.2.1. 未加锁版 4.2.2. 加锁版 1. 按 众所周知,对象是解决继承 ...

  5. Python 单例模式的几种实现方式

    单例模式的几种实现方式 先来看几个魔法方法的简单运用:__new__, __init__, __call__. class A(object): def __init__(self, x): prin ...

  6. python 单例模式

    单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在 用装饰器方式实现单例模式 #!/usr/bin/python # coding=utf-8 d ...

  7. Python单例模式

    1.单例模式介绍 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时, 单例对象的类必须保证只有一个实例存在.许多时候整个系统只需要拥有一个 全局对象,这样有利于我们协调系统整体的行为 ...

  8. python 单例模式的四种创建方式

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

  9. python 单例模式的四种实现方法及注意事项

    一.模块单例 Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码. #foo1.py clas ...

随机推荐

  1. 在VS2010中使用Git(转)

    在之前的一片博客<Windows 下使用Git管理Github项目>中简单介绍了在Windows环境中使用Git管理Github项目,但是是使用命令行来进行操作的,本文将简单介绍下在VS2 ...

  2. 界面上传文件js包【AjaxUpload.js】

    function uploadFile() { new AjaxUpload($("#importFile"), { action: url, type: "POST&q ...

  3. git环境搭建

    Linux kernel  的官方 GIT地址是: http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git 可以从这个地 ...

  4. Cadence ORCAD CAPTURE元件库介绍

    Cadence ORCAD CAPTURE元件库介绍 来源:Cadence 作者:ORCAD 发布时间:2007-07-08 发表评论 Cadence  OrCAD  Capture 具有快捷.通用的 ...

  5. ACdream 1726 A Math game (dfs+二分)

    http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...

  6. BZOJ 2173 整数的lqp拆分

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2173 题意:给出输出n.设一种拆分为n=x1+x2+x3,那么这种拆分的价值为F(x1) ...

  7. C# treeview控件部分节点添加checkbox

    一.先初始化treeview this.treeView1.CheckBoxes = true; this.treeView1.ShowLines = false; this.treeView1.Dr ...

  8. [HDOJ5521]Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 给n个点,m个块.块内点到点之间话费的时间ti.两个人分别从点1和点n出发,问两人是否可以相遇, ...

  9. leetcode:Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  10. 网易新闻页面信息抓取 -- htmlagilitypack搭配scrapysharp

    最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决定试一试~ 于是到https://www.nuget.org/packages/Scrapy ...