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)
原文地址:https://www.cnblogs.com/stin/p/8515756.html