Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a Singleton in python?

There are many questions related to the use of the Singleton pattern in python, and although this question might repeat many of the aspects already discussed, I have not found the answer to the following specific question.

Let's assume I have a class MyClass which I want to instantiate only exactly once. In python I can do this as follows in the code myclass.py:

class MyClass(object): 
    def foo(self):
        ....


instance = MyClass()

Then in any other program I can refer to the instance simply with

import myclass
myclass.instance.foo()

Under what circumstances is this approach enough? Under what circumstances is the use of a Singleton pattern useful/mandatory?

like image 319
Alex Avatar asked Jan 30 '13 08:01

Alex


People also ask

Why do we need singleton in Python?

Advantages of using the Singleton Method:Initializations: An object created by the Singleton method is initialized only when it is requested for the first time. Access to the object: We got global access to the instance of the object. Count of instances: In singleton, method classes can't have more than one instance.

When should a singleton be used?

It is used where only a single instance of a class is required to control the action throughout the execution. A singleton class shouldn't have multiple instances in any case and at any cost. Singleton classes are used for logging, driver objects, caching and thread pool, database connections.

Why do we use singletons?

The singleton pattern ensures that a class only has one instance and provides a global point of access to it. In other words, it restricts the instantiation of a class to one object.

Why should you avoid singletons?

By using singletons in your project, you start to create technical debt. Singletons tend to spread like a virus because it's so easy to access them. It's difficult to keep track of where they're used and getting rid of a singleton can be a refactoring nightmare in large or complex projects.


1 Answers

The singleton pattern is more often a matter of convenience than of requirement. Python is a little bit different than other languages in that it is fairly easy to mock out singletons in testing (just clobber the global variable!) by comparison to other languages, but it is neverthess a good idea to ask yourself when creating a singleton: am I doing this for the sake of convenience or because it is stricly necessary that there is only one instance? Is it possible that there may be more than one in the future?

If you create a class that really will be only constructed once, it may make more sense to make the state a part of the module, and to make its methods into module-level functions. If there is a possibility that the assumption of exactly one instance may change in the future, then it is often much better to pass the singleton instance around rather than referencing the singleton through a global name.

For example, you can just as easily implement a "singleton" this way:

if __name__ == '__main__':
   instance = MyClass()
   doSomethingWith(instance)

In the above, "instance" is singleton by virtue of the fact that it is constructed only once, but the code that handles it is provided the instance rather than referencing module.instance, which makes it easier to reuse pieces of the code if, in some future situation, you need more than one MyClass.

like image 173
Michael Aaron Safyan Avatar answered Oct 26 '22 13:10

Michael Aaron Safyan