python 单例模式实现

装饰器:

def singleton(cls):  
    instances = {}  
    def _singleton(*args, **kw):  
        if cls not in instances:  
            instances[cls] = cls(*args, **kw)  
        return instances[cls]  
    return _singleton  
 
@singleton  
class A(object):

 

posted @ 2017-09-13 20:28  闪电旅途  阅读(133)  评论(0编辑  收藏  举报