It’s pretty easy to implement a sloppy Singleton. You just need to hide the constructor and implement a static creation method.
The same class behaves incorrectly in a multithreaded environment. Multiple threads can call the creation method simultaneously and get several instances of Singleton class.
main.py: Conceptual example
classSingletonMeta(type):""" The Singleton class can be implemented in different ways in Python. Some possible methods include: base class, decorator, metaclass. We will use the metaclass because it is best suited for this purpose. """ _instances ={}def__call__(cls,*args,**kwargs):""" Possible changes to the value of the `__init__` argument do not affect the returned instance. """if cls notin cls._instances: instance =super().__call__(*args, **kwargs) cls._instances[cls]= instancereturn cls._instances[cls]classSingleton(metaclass=SingletonMeta):defsome_business_logic(self):""" Finally, any singleton should define some business logic, which can be executed on its instance. """# ...if__name__=="__main__":# The client code. s1 =Singleton() s2 =Singleton()ifid(s1)==id(s2):print("Singleton works, both variables contain the same instance.")else:print("Singleton failed, variables contain different instances.")
Output.txt: Execution result
Singleton works, both variables contain the same instance.