惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
博客园_首页
T
Tailwind CSS Blog
美团技术团队
博客园 - 叶小钗
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog

博客园 - Jaypei

archlinux中启用sshd [转] AT89C52资料 VirtualBox中安装PuppyLinux4 [转]比较全的Vim 中的正则表达式 luacurl的一个获得html的函数 [转]使用Mac OS X系统必须了解的10条命令 [原]Qt4.5中Plugins使用方法 用nuSOAP传递对象数组的问题终于解决 粗心导致的gsoap一个错误 MinGW环境使用gSOAP rdesktop中剪切板共享 [转]在Windows XP下用GCC 4.3.2编译Qt 4.4.3实战 Ubuntu下安装Postgresql 8.3 [转]如何讀取文字檔? (C/C++) (STL) 简单的python读写windows剪切板 关于SOAPpy传递对象参数调用WebService的问题总结 LinkedServer的用法 解决Smarty中trancate使用UTF8时中文乱码问题 关于wxPython中多线程修改主界面
[原创]关于python的Singleton
Jaypei · 2008-12-31 · via 博客园 - Jaypei

2008-12-31 15:09  Jaypei  阅读(487)  评论(2)    收藏  举报

总是想不到什么好办法在python里写一个完美的Singleton,参考了好多实例,想出了这么个办法

class Singleton(object):
    
__instance = Nonedef __new__(classtype, *args, **kwargs):
        
if classtype != type(classtype.__instance):
            classtype.
__instance = object.__new__(classtype, *args, **kwargs)
            classtype.
__instance.init()return classtype.__instancedef init(self):
        
pass

 这样既解决了子类化问题,又解决了__init__重复执行的问题。