Python单例模式

class Singleton(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
           cls.__instance = super(Singleton, cls).__new__(cls, *args, **kwargs)

        return cls.__instance
    
if __name__ == '__main__':

    # 多线程中单例的使用
    from threading import Thread
    def func():
        print(id(Singleton()))

for index in range(10000):
    Thread(target=func).start()

你可能感兴趣的:(Python单例模式)