A Singleton pattern ensures that a class can have only one instance. Such classes are used to store global data. The following code is copied from de.wikipedia.org.
#!/usr/bin/env python
class Singleton(object):
def __new__(type, *args):
if not '_the_instance' in type.__dict__:
type._the_instance = object.__new__(type)
return type._the_instance
def __init__(self):
if not '_ready' in dir(self):
self._ready = True
s1 = Singleton()
s2 = Singleton()
print( "id(s1)", id( s1))
print( "id(s2)", id( s2))