python的一种单例模式

单例模式:一个类无论创建多少个对象,其对象都是同一对象即在内存中的地址为同一地址

在python中的实现方法

class singleton:

        _instance = None

        def  __new__(cls,*args,**kwargs):

                if cls._instance == None:

                            cls._instance = object.__new__(cls,*args,**kwargs)

                return cls._instance

A = singleton()

B = singleton()

print(A)

print(B)

 

这是最简单的一种单例模式的实现方法

猜你喜欢

转载自blog.csdn.net/Python_life/article/details/81175589
今日推荐