Python3-单例模式

 

单例模式

  单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。

 

 1 class Singleton:
 2     __instance = None
 3 
 4     def __new__(cls,*args,**kwargs):
 5         if cls.__instance == None:
 6             cls.__instance = object.__new__(cls)
 7 
 8         return cls.__instance
 9 
10 s = Singleton()
11 print(s)   # <__main__.Singleton object at 0x000001DDB4377BE0>
13 s1 = Singleton()
14 print(s1)   # <__main__.Singleton object at 0x000001DDB4377BE0>

 

posted @ 2020-06-02 11:10  闹点小情绪q  阅读(363)  评论(0编辑  收藏  举报