Singletons
class MySingleton(object): # object because we are inheriting from the baseclass object
_instance = None
def __new__(self):
if not self._instance:
self._instance = super(MySingleton, self
).__new__(self)
self.y = 10
return self._instance
x = MySingleton()print(x.y)
>> 10
x.y = 20
z = MySingleton()
print(z.y)
>> 20Singleton as Decorator
Last updated