您的位置:首页 > 编程语言 > Python开发

[原创]关于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__重复执行的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: