python 之单例

 

 

# 单例模式
class MySQL:
    __instance = None
    def __init__(self):
        self.host = '127.0.0.1'
        self.port = 3306

    @classmethod
    def singleton(cls):
        if not cls.__instance:
            obj = cls()
            cls.__instance = obj
        return cls.__instance

    def conn(self):
        pass

    def execute(self):
        pass

obj1 = MySQL.singleton()
obj2 = MySQL.singleton()
print(obj1 == obj2)

 

posted @ 2018-03-06 17:45  Claire_xu  阅读(105)  评论(0编辑  收藏  举报