单例模式

#######单例模式


class TestSingle(object):
    """使用父类的new方法初始换一个实例。
       new方法会自动接收类创建时的参数,所以要设定不定长的参数
    """
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = super(TestSingle, cls).__new__(cls, *args, **kwargs):
        return cls._instance

原文地址:https://www.cnblogs.com/by2016/p/6916305.html