[原创]关于python的Singleton
2008-12-31 15:09
344 查看
总是想不到什么好办法在python里写一个完美的Singleton,参考了好多实例,想出了这么个办法
class Singleton(object):
__instance = None
def __new__(classtype, *args, **kwargs):
if classtype != type(classtype.__instance):
classtype.__instance = object.__new__(classtype, *args, **kwargs)
classtype.__instance.init()
return classtype.__instance
def init(self):
pass
这样既解决了子类化问题,又解决了__init__重复执行的问题。
class Singleton(object):
__instance = None
def __new__(classtype, *args, **kwargs):
if classtype != type(classtype.__instance):
classtype.__instance = object.__new__(classtype, *args, **kwargs)
classtype.__instance.init()
return classtype.__instance
def init(self):
pass
这样既解决了子类化问题,又解决了__init__重复执行的问题。
相关文章推荐
- 关于Python中函数重载问题的思考(原创)
- 关于python的_init_()方法的一点解释
- python中关于 and 与or的优先级举例
- 关于对学生大本营seo策略及csdn技术搜索引擎的建议(原创)
- 原创python多线程批量管理工具batch(不断完善)
- 关于SQLSERVER数据操作的常用类库(原创)
- selenium2.0关于python的常用函数(一)
- 关于pythonfor循环语句
- Python 网络爬虫--关于简单的模拟登录实例讲解
- 应该写点关于python的东西才是
- Go学习笔记:关于Java、Python、Go编程思想的不同
- 【原创】Python处理海量数据的实战研究
- 轻松python之文件专题-关于行的操作
- 调试用Python C API 写的程序问题还真多,关于import搜索路径的,复制过来,以防忘记
- python 关于yield, generator, iterable
- 关于Python的super用法研究
- 关于python爬虫
- Python关于__init__.py文件用法
- (原创)关于继承实例化的说明(c#)
- 关于LeaderShip, 说说你身边这样的Leader吗? [原创翻译]