class MetaSingleton(type):
def __init__(self,*args,**kwargs):
self._instance = None
super(MetaSingleton,self).__init__(*args,**kwargs)
def __call__(self, *args, **kwargs):
if self._instance is None:
self._instance = super(MetaSingleton,self).__call__(*args,*kwargs)
return self._instance
class Singleton1(object,metaclass=MetaSingleton):
pass
s1 = Singleton1()
s2 = Singleton1()
print('singleton1',s1 is s2)
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)