python单例模式

class Person():
    __instance=None
    @classmethod
    def get_instance(cls):
        if cls.__instance:
            return cls.__instance
        else:
            cls.__instance=Person()
            return cls.__instance
        

if __name__=='__main__':
    p1=Person.get_instance()
    p2=Person.get_instance()
    p3=Person.get_instance()
    print(p1)
    print(p2)
    print(p3)

 

posted @ 2019-12-25 20:12  Mars.wang  阅读(117)  评论(0编辑  收藏  举报