单例 -- 同一个实例化的对象

class C:
    __instance = None

    def __init__(self):
        pass

    @classmethod
    def instance(cls):
        if cls.__instance is None:
            cls.__instance = C()
        return cls.__instance

#obj obj1 obj2 都是同一个对象
obj = C.instance()
obj1 = C.instance()
obj2 = C.instance()

 

posted @ 2018-08-24 15:57  运维00001  阅读(138)  评论(0)    收藏  举报