单例

class Single:
    __ISINSTANCE=None
    def __new__(cls, *args, **kwargs):#构造方法 在实例化对象之前在__init__()之前执行
        #第一个参数cls是当前正在实例化的类。
        if not cls.__ISINSTANCE:
            cls.__ISINSTANCE=object.__new__(cls)#当前类是直接继承自 object
               #当前类的 __new__() 方法返回的对象应该为return object.__new__(cls)
        return cls.__ISINSTANCE

    def __init__(self,name,age):
        self.name=name
        self.age=age

s1=Single('京东',6)
s2=Single('天猫',5)
print(s1.name)
print(s2.name)
原文地址:https://www.cnblogs.com/long-holiday/p/9903697.html