In coding, there exists a Pattern called Singleton
, restricting the instantiation of a class to only one single object. But what are the reasons to use a Singleton, if one can just instantiate the class once on module level, and e.g. name it with an underscore and use just that? Why and when should a Singleton be used?
To be specific (as no answer really is satisfying for me) here is a concrete example. I specify a standard python class and create an instance:
class MyObject(object):
....
_myinstance = MyObject()
Besides the fact that this implementation does not prevent the creation of a second instance, under what circumstances is the use of a Singleton better than this simple implementation?